Web Design that taps into the haromny and vision of your dreams.

Introduction to Programming with C# 7

By on in Coding

1,537 words, estimated reading time 7 minutes.

Introduction to Programming with C# Series
  1. Introduction to Programming with C# 7
  2. C# Programming Fundamentals
  3. Introduction to Object-Oriented Programming
  4. C# Object-Oriented Programming Part 2
  5. Flow Control and Control Structures in C#
  6. C# Data Types, Variables and Casting
  7. C# Collection Types (Array, List, Dictionary, Hash Table)
  8. C# Operators
  9. Using Data in C# 7 with ADO.Net & Entity Framework
  10. LINQ: .NET Language Integrated Query
  11. Error and Exception Handling in C#
  12. Advanced C# Programming Topics
  13. Reflection in C#
  14. What Are ASP.Net Webforms
  15. Introduction to ASP.Net MVC
  16. Windows Application Development
  17. Assemblies in C#
  18. Working with Resources Files, Culture & Regions
  19. Regular Expressions in C#
  20. Introduction to XML with C#
  21. Complete Guide to File Handling in C#

Computer Programming is a profession where somebody writes a set of instructions for a computer to process and return a result. You may be very surprised to learn that the first computer program was written in 1842 by Ada Lovelace, daughter of the famous poet Lord Byron.

Programming is often thought of as a geeky and nerdy profession, but it is also obvious that creating software requires a certain amount of creativity. At the highest level, it must be clever but not complex, intuitive and easy to use. At the lower levels, creativity also plays an important role. Every line of code is like poetry, every instruction requires consideration of what came before it, and what comes after it. In this respect, programming is like writing a book or painting a masterpiece. Ultimately you have a finished product, but it's the sentences and the brush strokes combined, which is where the book, masterpiece or program truly comes to life.

What is a Program?

A program is made up of statements, often likened to sentences and called "a line of code". On its own, it has structure and purpose, but without the context of the other statements around it, it isn't meaningful. Like a book, a program runs from the first statement to the last, at which point it finishes and is closed. Unlike a book, the program can repeat itself, jump backwards and forwards through the chapters, return to the start or jump to the end.

Groups of statements are known as blocks of code and are often separated into distinct functions. A function is a block of code with a specific job to do that can be re-used anywhere else. When a function is used, we often say that it is "called". An example would be a program to calculate income tax for five people. Rather than writing the tax calculation code five times - one for each calculation - we can write the code once, turn it into a function, and call that function 5 times. This not only saves a lot of typing, but it also ensures that each of the five calculations is performed in exactly the same way. We can also call the function 10 times, 100 times or even 1,000,000 times without having to write it over and over again.

Programming Languages

A programming language is used to write the program, much like the language used to write a book. Although the fundamental concepts and general practices remain the same, the overall grammar (programmers call grammar syntax) changes between languages. Some programming languages are very "human" orientated and are written as if you were saying an instruction, while others are a bit more convoluted and require a bit of thought as to what they are doing. The BASIC programming language is one of the easiest to start learning as it is one of the most human orientated, although it is limited. C and C++ are some of the more powerful languages and they allow you to do a lot more than BASIC, however, they are much more difficult to understand and to write.

Microsoft C# is a relatively new language developed specifically for the modern era and combines aspects of various languages to form an easy to use language, but also powerful. It combines the ease of use of BASIC and Pascal with the flexibility and power of C and C++ which have been around since the early 70's.

Microsoft .Net

The C# language runs under the Microsoft .Net Framework. This provides the necessary compile-time and run-time platform to build and run .Net based applications as well as a set of technologies that enable a common environment for all applications written using a .Net language. The .Net Framework is designed with ease of use and code reuse in mind and features many programming languages that can interact with each other.

There are many other languages that support the .Net framework, such as Visual Basic, Perl, Java, J# and Pascal (Delphi). This website deals specifically with the C# language as it is the most common, and easiest to learn.

The Following diagram shows how the .Net framework is structured and how each platform interacts with those around it.

Overview of the .Net Platform
Overview of the .Net Platform

Common Language Specification

The Common Language Specification (CLS) defines how the programming language interacts with objects and how objects interact with other objects. The CLS also provides a single set of data types for all languages, using a component called the Common Type Specification (CTS). If you have done any C++ programming you will be familiar with the C++ string being a character pointer (PChar or char*). In .Net a string is the same in all languages, so a Visual Basic application can call a method in a C# class library without the need for type conversion.

On the .Net platform, you don't have one rule describing how objects in C# behave and another rule describing how they should behave in Visual Basic. To steal a phrase, there is now "One rule to bind them all". Objects in Visual Basic and C# share the same methods and properties, same class behaviour and the same lifecycle.

In existing models, API's are used to interface with the Operating System or hardware. There was no consistency between different interfaces, parameter types had to be converted between languages and return types were often pointers to memory locations. With the introduction of the .Net framework everything is now part of a common system, and the nightmare of API programming is finally over.

Common Base Class Library

Taking the Common Type Specification one step further, the .Net platform provides a set of common classes. This also allows for code compatibility between languages. A button class in C# is the same as a button class in VB.Net or ASP.Net.

The Base Class Library also defines hundreds of classes, which you can use in your applications, so you spend less time working on the class development and more time on productivity and functionality.

Common Language Runtime

Traditionally a software application written for the Windows environment would need to be completely re-written, sometimes in a different programming language, in order for it to be able to run on a handheld device or other hardware platform. The .Net environment removes this re-coding and allows code to be used on any hardware platform supported by the framework. This technology, or component, of the .Net framework is called the Common Language Runtime (CLR). The CLR provides a managed environment for code execution, which performs certain maintenance functions and memory management. These are covered in more depth in the Common Language Runtime tutorial.

Your First Console Application in C#

Now that we have introduced the .Net framework and C#, lets dive in and create our first infamous "Hello World" program. If you haven't already installed Visual Studio and configured your environment the following tutorial should help you.

Go ahead and open up the Visual Studio and create a new Console Application. This can be done from the menus File » New » Project, then from the left-hand menu select Visual C# and Console Application (.Net framework) on the right-hand side.

In order to keep things as simple as possible for the first lesson, select all the code Ctrl+A or Edit » Select All, and delete it. Next, you need to type in or copy and paste the following code into the editor.

using System;
 
class HelloWorld
{
  static void Main()
  {
    Console.WriteLine("Hello World!");
  }
}

You can now run the program by pressing Ctrl+F5 which will start without debugging, or from the menu select Debug » Build Without Debugging. You should see the console window which says "Hello World!"

Hello World Output
Hello World Output

Let's Examine the Code

On the first line, we are telling the compiler that we want to use something called System. The system is a namespace that is provided by Microsoft. Namespaces are an important concept in .Net and will be covered in an upcoming tutorial.

The next line of code is creating a class called HelloWorld. Again, a class is a fundamental concept in object orientated programming and will be covered in great detail in future tutorials. For now, think of a class as a container. Everything must be contained within a class in C#, unlike C/C++ and PHP where you can have variables inside or outside a class or function.

The next line of code defines the Main function for the program. This is the main entry point for the application and where all your code will be called from. Finally, our last line of code simply tells the program to write a line onto the screen.

Console.WriteLine is used to write a line of text to the console screen and automatically add a newline and carriage return to the end. Notice there is a semi-colon at the end of the line. C#, like a lot of programming languages, C# uses the semi-colon to separate lines of code or operations and every code statement will have a semi-colon at the end.

Conclusions

In this C# tutorial, we have had a quick introduction to programming, what the .Net framework is and written a very basic C# console application called "Hello World!". We have introduced a few concepts which we will expand on in future tutorials.

In the next tutorial, we will look at some of the basics of programming, introduce variables, data types, and functions, as well as building upon some of the concepts we saw here.

Last updated on: Thursday 11th October 2018

 

Comments

Have a question or suggestion? Please leave a comment to start the discussion.

DoktorThomas™

DoktorThomas™

You write, "I have written a series of tutorials for learning C# which should help you get started in programming using a modern, easy to use language."
Where is the link?

Reply to DoktorThomas™
Tim Trott

Tim Trott

Below the article. For some reason they were not showing, but they are there now.

Thanks for pointing that out to me :)

Reply to Tim Trott

 

Leave a Reply

Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Your email address will not be published.