Learn to Program in C# - Full Introduction to Programming CourseIntroduction to Programming is intended for beginners who have little or no computer programming experience. This is a complete guide on learning basics of programming.
This article is part of a series of articles. Please use the links below to navigate between the articles.
- Learn to Program in C# - Full Introduction to Programming Course
- Introdution to Programming - C# Programming Fundamentals
- Introduction to Object Oriented Programming for Beginners
- Introduction to C# Object-Oriented Programming Part 2
- Application Flow Control and Control Structures in C#
- Guide to C# Data Types, Variables and Object Casting
- C# Collection Types (Array,List,Dictionary,HashTable and More)
- C# Operators: Arithmetic, Comparison, Logical and more
- Using Entity Framework & ADO.Net Data in C# 7
- What is LINQ? The .NET Language Integrated Query
- Error and Exception Handling in C#
- Advanced C# Programming Topics
- All About Reflection in C# To Read Metadata and Find Assemblies
- What Are ASP.Net WebForms
- Introduction to ASP.Net MVC Web Applications and C#
- Windows Application Development Using .Net and Windows Forms
- Assemblies and the Global Assembly Cache in C#
- Working with Resources Files, Culture & Regions in .Net
- The Ultimate Guide to Regular Expressions: Everything You Need to Know
- Introduction to XML and XmlDocument with C#
- Complete Guide to File Handling in C# - Reading and Writing Files
This Introduction to Programming course is intended for beginners who have little or no computer programming experience. It will take you through the very basics and explain the core concepts of programming. We will then move on to some more advanced techniques as you learn to code Windows applications, websites, use databases and more!
Introduction to Programming - What is Programming?
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 how the sentences and the brush strokes combined, which is where the book, masterpiece or program truly comes to life.
What is a Computer Program?
A computer 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. A function can also be called a method or procedure.
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 also ensures that each of the five calculations is performed in 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.
What are 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 syntax (the programming equivalent of grammar) 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.
What is 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.
Many other languages 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.
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, the 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 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 rewritten, sometimes in a different programming language, for it to be able to run on a handheld device or another 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.
Writing Your First Console Application in C#
Now that we have introduced the .Net framework and C#, let's dive in and create our first infamous "Hello World" program.
Go ahead and open up 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.
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!"
Let's Examine What the Code is Doing
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 of 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 new line 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 a semi-colon to separate lines of code or operations and every code statement will have a semi-colon at the end.
What Did We Learn in the First Lesson?
In this C# tutorial, we have had a quick introduction to programming, what the .Net framework is and we have 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.