Introduction to C#

1. Introduction to C#

C# is a general-purpose, modern and object-oriented programming language pronounced as “C sharp”. It was developed by Microsoft led by Anders Hejlsberg and his team within the .Net initiative and was approved by European Computer Manufacturers Association (ECMA) and International Standards Organization (ISO). C# is among the languages for Common Language Infrastructure and the current version of C# is version 7.2. C# is a lot similar to Java syntactically and is easy for the users who have knowledge of C, C++ or Java.

2. Advantages of Learning C#

  1. Easy to start: C# is a high-level language so it is closer to other popular programming languages like C, C++, and Java and thus becomes easy to learn for anyone.
  2. Widely used for developing Desktop and Web Application: C# is widely used for developing web applications and Desktop applications. It is one of the most popular languages that is used in the professional desktop. If anyone wants to create Microsoft apps, C# is the go to language.
  3. Community: The larger the community the better it is as new tools and software will be developing to make it better. C# has a large community so the developments are done to make it exist in system and not become extinct.
  4. Game Development: C# is widely used in game development and will continue to dominate. C# integrate with Microsoft and thus has a large target audience. The C# features such as Automatic Garbage Collection, interfaces, object-oriented etc. makes C# a popular game developing language.

3. Programming in C#:

Since the C# is a lot similar to other widely used languages syntactically, it is easier to code and learn in C#.
Programs can be written in C# in any of the widely used text editors like Notepad++, gedit etc. or on any of the compilers. After writing the program save the file with the extension .cs.

Example: A simple program to print Hello World

// C# program to print Hello World

using System;

namespace HelloWorldApp
{  
    class HelloWorld
    {  
        // Main function
        static void Main(string[] args)
        {

            // Printing Hello World
            Console.WriteLine("Hello World");

            Console.ReadKey();
        }
    }
}

Explanation:

  • Comments: Comments are used for explaining the code and are used in a similar manner as in Java or C or C++. Compilers ignore the comment entries and do not execute them. Comments can be of a single line or multiple lines.
           Single line Comments:
           Syntax: 
           // Single line comment

           Multi line comments:
           Syntax:
           /* Multi line comments*/


  • using System: using keyword is used to include the System namespace in the program.
  • namespace declaration: A namespace is a collection of classes. The HelloWorldApp namespace contains the class HelloWorld.
  • class: The class contains the data and methods to be used in the program. Methods define the behavior of the class. Class HelloWorld has only one method Main similar to Java.
  • static void Main(): static keyword tells us that this method is accessible without instantiating the class. 
  • void keywords tell that this method will not return anything. Main() method is the entry point of our application. In our program, Main() method specifies its behavior with the statement Console.WriteLine(“Hello World”); .
  • Console.WriteLine(): WriteLine() is a method of the Console class defined in the System namespace.
  • Console.ReadKey(): This is for the VS.NET Users. This makes the program wait for a key press and prevents the screen from running and closing quickly.

Note: C# is case sensitive and all statements and expressions must end with a semicolon (;).