Sunday, 29 May 2016

Skeleton of a C# program


  1. A C# programs can contain one or more files. 
  2. Each program file can contain zero to N number of namespaces. 
  3. A namespace can contain types such as classes, interfaces, enumerations, structs and delegates. 
  4. Skeleton of a C# program that contains all of the above elements.


C#

using System;
namespace NameOfYour_Namespace
{
    class NameOfYour_Class
    {
    }

    struct NameOfYour_Struct
    {
    }

    interface NameOf_IYourInterface 
    {
    }

    delegate int NameOfYour_Delegate();

    enum NameOfYour_Enum 
    {
    }

    namespace NameOfYour_NestedNamespace
    {
        struct NameOfYour_Struct 
        {
        }
    }

    class NameOfYour_MainClass
    {
        static void Main(string[] args) 
        {
            //Do something
        }
    }
}

My First C# 'Hello World' Program

Create "Hello world" program in console application

Steps

1. Open Visual Studio.
2. From the menu -> choose File -> New -> Project.
3. The New Project dialog box will open.
Choose Visual C# from left side panel and then choose Console Application.
4. Provide a name for your project
5. Choose the OK button.
6. You can see in the right side panel that the new project appears in Solution Explorer.
7. Open the program.cs file
8. Type the following code in the code window

C#

using System;
namespace HelloWorld
{
    class Hello 
    {
        static void Main() 
        {
            Console.WriteLine("My first C# application");
            Console.WriteLine("Hello World!");
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
    }
}

9. Press F5 key to run the project
10. A Command Prompt window will appear that contains the output as below

Output:
My first C# application
Hello World!
Press any key to exit