How to write the first program in C#?


The following is the first program in C# programming −

Example

 Live Demo

using System;

namespace MyHelloWorldApplication {

   class MyDemoClass {

      static void Main(string[] args) {

         // display text
         Console.WriteLine("Hello World");

         // display another text
         Console.WriteLine("Welcome!");
         Console.ReadKey();
      }
   }
}

Output

Hello World
Welcome!

Let us see now what all it includes.

  • using System; - the using keyword is used to include the System namespace in the program.

  • namespace declaration - A namespace is a collection of classes. The MyHelloWorldApplication namespace contains the class MyDemoClass.

  • The class MyDemoClass contains the data and method definitions that your program uses. Classes generally contain multiple methods.

  • Main method is the entry point for all C# programs. The Main method states what the class does when executed.

  • 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. Add more text using the same Console.WriteLine(); in C#.

  • Console.ReadKey(); It 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.

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 20-Jun-2020

418 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements