The C# Programming Language - A full Guide with Examples


  • Setup
  • Your First C# Program
  • Types and Variables
  • Flow Control Statements
  • Operators
  • Strings
  • Classes, Objects, Interface and Main Methods
  • Fields and Properties
  • Scope and Accessibility Modifiers
  • Handling Exceptions


  • Generics
  • Events, Delegates and Lambda Expressions
  • Collection Framework
  • LINQ


  • Asynchronous Programming (Async and Await)
  • Task Parallel Library

What’s New in C# 6

  • Null-Conditional Operator
  • Auto-Property Initializers
  • Nameof Expressions
  • Expression Bodied Functions and Properties
  • Other Features

Object-Oriented principles (OOP)

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

Solid principles

  • Single Responsibility Principle
  • Open Closed Principle
  • Liskov Substitution Principle
  • Interface Segregation Principle
  • Dependency Inversion Principle

C# Best practices, Design Patterns & Test Driven Development (TDD)


LinqPad is an .NET scratchpad to quickly test your C# code snippets.The standard edition is free and a perfect tool for beginners to execute language statements, expressions and programs.

Alternatively, you could also download Visual Studio Community 2015 which is an extensible IDE used by most professionals for creating enterprise applications.

Your First C# Program

//this is the single line comment

/** This is multiline comment,
compiler ignores any code inside comment blocks.

//This is the namespace, part of the standard .NET Framework Class Library
using System;
// namespace defines the scope of related objects into packages
namespace Learning.CSharp
  // name of the class, should be same as of .cs file
  public class Program
    //entry point method for console applications
   public static void Main()
      //print lines on console
      Console.WriteLine("Hello, World!");
      //Reads the next line of characters from the standard input stream.Most common use is to pause program execution before clearing the console.

Every C# console application must have a Main method which is the entry point of the program.

Edit HelloWorld in .NET Fiddle, a tool inspired by JSFiddle where you can alter the code snippets and check the output for yourself. Note, this is just to share and test the code snippets, not to be used for developing applications.

If you are using visual Studio, follow this tutorial to create console application and understand your first C# program.

Types and Variables

C# is a strongly typed language. Every variable has a type. Every expression or statement evaluates to a value. There are two kinds of types in C#

  • Value types
  • Reference types.

Value Types : Variables that are value types directly contain values. Assigning one value type variable to another copies the contained value.

Edit in .NET Fiddle

int a = 10;
int b = 20;
Console.WriteLine(a); //prints 20
Console.WriteLine(b); //prints 20

Note that in other dynamic languages this could be different, but in C# this is always a value copy. When value type is created, a single space most likely in stack is created, which is a “LIFO” (last in, first out) data structure. The stack has size limits and memory operations are efficient. Few examples of built-in data types are int, float, double, decimal, char and string .

Type Example Description
Integer int fooInt = 7; Signed 32-bit Integer
Long long fooLong = 3000L; Signed 64-bit integer. L is used to specify that this variable value is of type long/ulong
Double double fooDouble = 20.99; Precision: 15-16 digits
Float float fooFloat = 314.5f; Precision: 7 digits . F is used to specify that this variable value is of type float
Decimal decimal fooDecimal = 23.3m; Precision: 28-29 digits .Its more precision and smaller range makes it appropriate for financial and monetary calculations
Char char fooChar = 'Z'; A single 16-bit Unicode character
Boolean bool fooBoolean = false; Boolean - true & false
String string fooString = "\"escape\" quotes and add \n (new lines) and \t (tabs); A string of Unicode characters.

For complete list of all built-in data types see here

Reference types : Variables of reference types store references to their objects, which means they store the address to the location of data on the stack, also known as pointers. Actual data is stored on the heap memory. Assigning reference type to another doesn’t copy the data, instead it creates the second copy of reference which points to the same location on the heap.

In heap, objects are allocated and deallocated in random order that is why this requires the overhead of memory management and garbage collection.

Unless you are writing unsafe code or dealing with unmanaged code, you don’t need to worry about the lifetime of your memory locations. .NET compiler and CLR will take care of this, but it’s still good to keep this mind in order to optimize performance of your applications.

More information here

Flow Control Statements

int myScore = 700;
if (myScore == 700)
    Console.WriteLine("I get printed on the console");
else if (myScore > 10)
    Console.WriteLine("I don't");
    Console.WriteLine("I also don't");

/** Ternary operators
 A simple if/else can also be written as follows
 <condition> ? <true> : <false> **/
int myNumber = 10;
string isTrue = myNumber == 10 ? "Yes" : "No";
using System;

public class Program
    public static void Main()
        int myNumber = 0;
        switch (myNumber)
            // A switch section can have more than one case label.
            case 0:
            case 1:
                Console.WriteLine("Case 0 or 1");

            // Most switch sections contain a jump statement, such as a break, goto, or return.;
            case 2:
                Console.WriteLine("Case 2");
            // 7 - 4 in the following line evaluates to 3.
            case 7 - 4:
                Console.WriteLine("Case 3");
            // If the value of myNumber is not 0, 1, 2, or 3 the
            //default case is executed.*
                Console.WriteLine("Default case. This is also optional");
                break; // could also throw new Exception() instead
for (int i = 0; i < 10; i++)
  Console.WriteLine(i); //prints  0-9

for (int i = 0; i <= 10; i++)
  Console.WriteLine(i); //prints  0-10

for (int i = 10 - 1; i >= 0; i--) //decrement loop
  Console.WriteLine(i); //prints 9-0

for (; ; )
// All of the expressions are optional. This statement
//creates an infinite loop.*
// Continue the while-loop until index is equal to 10.
int i = 0;
while (i < 10)
    Console.Write("While statement ");
    Console.WriteLine(i);// Write the index to the screen.
    i++;// Increment the variable.

int number = 0;
// do work first, until condition is satisfied i.e Terminates when number equals 4.
    Console.WriteLine(number);//prints the value from 0-4
    number++; // Add one to number.
} while (number <= 4);

Are you a C# programmer?

No I am not, I have no experience on it.

Well just wanted to say thanks for bringing this on here… I recently learned C# and I love it, Im looking for other people who want to do a project with C# so if anyone does, contact me ASAP. Even if you don’t know much about it and want to learn, I’d be down to show you some stuff. Thanks!

P.S. Linqpad is awesome! Thanks for that resource!

Well you can also try our chat room if you want to look for other people. We don’t have a C# room but you could try @TheOnlyRealTodd

###Great post on C#!

I’ve been developing with it for a while now, glad to see it getting some notice here on the forums. I just got started on FreeCodeCamp today to work on my front-end skills, since I wanted to tweak my blog’s look.

Earlier this week I actually wrote a post about getting started on C#:

I discuss a few resources to get people started, if they are looking for more after your introduction :slight_smile:

1 Like

i would be interested in doing a project with C#. I do know a little bit of C# but i haven’t used it extensively as i started with FCC midway and since then my sole focus was on Javascript.

Try this tutorial…

Simple and easy…


I am midway in the Front-End, but when I do the Back-End I will be using using Node.js and ASP.Net MVC (C#) to complete projects. Hopefully, I should be able to start the Back-End projects in February or March. Holidays will make a big impact on my learning.

I have been do ASP.Net MVC development for the last couple of years.

.NET Academy
Learn new .NET skills in a fun and interactive way.

C# (pronounced C sharp) is a new programming language designed for building a wide range of enterprise applications that run on the .NET Framework. An evolution of Microsoft C and Microsoft C++, C# is simple, modern, type safe, and object oriented.

I would be keen to do C# projects, if we have a team in freeCODEcamp