C# Tutorial

Developed by Microsoft, C# Programming language is an object oriented, simple programming language allowing programmers to develop various kinds of applications.

C# Tutorial

Learn C# with our complete and interesting C# Tutorial. Starting with C# basics, this tutorial explains you the advance concepts very easily with the help of practical examples and C# programs.

Who is this C# Tutorial designed for?

This tutorial will be very useful for both beginners and professionals. All the freshers, BCA, BE, BTech, MCA and college students, will find this tutorial extremely useful for their notes, exam preparation, lab exercises, assignments and viva questions.

What do I need to know to begin with?

A basic knowledge of C or C++ will come quite helpful to get the maximum of this tutorial

C# syllabus covered in this tutorial

This C# tutorial covers:

Overview of C#, Data Types, enum, const, Structure, Class, Constructors, Properties, Destructors, Event & Delegate, Lambda Expression, Object Initializer, Collection Initializer, Extension Methods, Inheritance, Virtual and Abstract, Interface, Collections, Dictionary classes, Generic Collections, Exception Handling, I/O, Serialization, Namespaces, Multithreading, LINQ

This covers almost everything in C# Programming. So, let's begin learning!

Introduction

C# is an object-oriented programming language developed by Microsoft by Anders Hejlsberg and his team.

Prerequisites
You should have basic understanding of C or C++ programming. It is very much based on C and C++ programming languages.

Main features of C#

Simple
Most of the confusing and complicated things are removed in C#, such as there is no usage of scope resolution operator "::" or "->" operators. Unsafe operations such as direct memory manipulation are not allowed. There is no need to handle memory explicitly. It supports automatic memory management and garbage collection. Next important change is that, "= =" is used for comparison operation and "=" is used for assignment operation.

assignment operation (=) cannot be used in "if" or “while” statement. If you use, It will give compile time error.

Object oriented
C# supports all features of object oriented language such as class, object, Data Encapsulation, inheritance, polymorphism, interfaces.

Type safe
Pointers are not directly supported by .NET. C# - Types are safe. Type-safe code accesses only the memory locations it is authorized to access.

Example:

string empName = 10; //cause an error
int a = "Hello World"; // cause an error
double db = d; //cause an error


All of the above code will give compile time error.
While accessing the array, C# checks the range and warned if it goes out-of-bounds.

Interoperability
C# includes native support for the COM and windows based applications.
We can easily access the Components (dll) from VB. NET and other managed code languages using C# and vice versa.

Scalable and updateable
We can upgrade code of an existing running application by using the new version of dll.
There is no registering of dynamic linking library.

Application of C#

C# language can be used with variety of applications
  • Web application
  • Windows application
  • Web services
  • Windows services
  • Mobile application
  • Developing component library
Some of the important features added in successors versions:

VersionYearFeature
1.02002Object Oriented, Simple, Typesafe, Managed, Garbage Collection, Cross-platform
2.02005Generics, Partial types, Anonymous methods, Nullable types, Getter/setter separate accessibility, Static classes, Delegate inference
3.02008Implicitly typed local variables, Object and collection initializers, Auto-Implemented properties, Anonymous types, Extension methods, Query expressions, Lambda expressions, Expression trees, Partial methods
4.02010Named and optional arguments, Tuples, Generic co- and contravariance
5.02012Asynchronous method, Caller info attributes
6.02015Compiler-as-a-service (Roslyn), Exception filters,  Await in catch/finally blocks, Auto property initializers, Default values for getter-only properties, String Interpolation, nameof operator, Dictionary initializer

Getting Started with C# program

Let us directly jump on our simple example that will display “Welcome at TutorialRide.com” on the console.

using System; // Namespace Declaration
namespace ConsoleApplication1
{
    class Program
    {
       // program starts with a call to Main() function.
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome at TutorialRide.com");
            System.Console.ReadLine();
        }
    }
}


Compile the above program; it will create the ConsoleApplication1.exe file.

C# program starts with namespaces. Namespaces are the logical grouping of related types. Console class is available in System namespace. It is not compulsory to use name space, but at this situation you have to append the related namespace with every types.

Example:

System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.Console.ReadLine();                 // fully qualified name.


As you can see, the code is lengthy; therefore it would be nice if you write required namespaces at the beginning of program.

In C# code must be contained within a class, so when you create a new console application in Visual Studio, the template will be created automatically that have a class and main function is written.

Important: C # is a case-sensitive language.
For writing text on console window, there are two static methods of Console class Write() and WriteLine().

Console.ReadLine() reads user input. ReadLine() is also an static method and return type is strings. If you execute the console application without writing the Console.ReadLine(), then console window will disappear in seconds and you will not able to see the output. It forces the application to wait for some input from keyboard before the application exits.