Introduction to C# Programming

C# is a simple, modern, general-purpose, object-oriented programming language developed by Microsoft within its .NET initiative led by Anders Hejlsberg. This eBook will teach you basic C# programming and will also take you through various advanced concepts related to C# programming language.
C# programming is very much based on C and C++ programming languages, so if you have a basic understanding of C or C++ programming, then it will be fun to learn C#.

Simple C# Program to display Hello World

Output of above coding

The following reasons make C# a widely used professional language

  • It is a modern, general-purpose programming language
  • It is object oriented.
  • It is component oriented.
  • It is easy to learn.
  • It is a structured language.
  • It produces efficient programs.
  • It can be compiled on a variety of computer platforms.
  • It is a part of .Net Framework.

    The .Net Framework

    The .Net framework is a revolutionary platform that helps you to write the following types of applications.

    • Windows applications
    • Web applications
    • Web services

    Integrated Development Environment (IDE) for C#

    Microsoft provides the following development tools for C# programming. (Latest version can be used as the requirement)

    • Visual Studio 2012 (VS)
    • Visual C# 2012 Express (VCE)
    • Visual Web Developer

    Creating Hello World Program

    This C# program consists of the following parts.

    • Namespace declaration
    • A class
    • Class methods
    • Class attributes
    • A Main method
    • Statements and Expressions
    • Comments

    Let us look at a simple code that prints the words "Hello World"

    Output of above coding

    Let us look at the various parts of the above program

    • The first line of the program using System; - the using keyword is used to include the System namespace in the program. A program generally has multiple using statements.
    • The next line has the namespace declaration. A namespace is a collection of classes. The HelloWorldApplication namespace contains the class HelloWorld.
    • The next line has a class declaration, the class HelloWorld contains the data and method definitions that your program uses. Classes generally contain multiple methods. Methods define the behavior of the class. However, the HelloWorld class has only one method Main.
    • The next line defines the Main method, which is the entry point for all C# programs. The Main method states what the class does when executed.
    • The next line // is ignored by the compiler and it is put to add comments in the program.
    • The Main method specifies its behavior with the statement Console.WriteLine("Hello World");
    • WriteLine is a method of the Console class defined in the System namespace. This statement causes the message "Hello, World!" to be displayed on the screen.
    • The last line Console.ReadKey(); is for the VS.NET Users. This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.

    It is worth to note the following points

    • C# is case sensitive.
    • All statements and expression must end with a semicolon (;).
    • The program execution starts at the Main method.
    • Unlike Java, program file name could be different from the class name.

    Leave a Reply

    Your email address will not be published. Required fields are marked *