Beginning C# programming with Hello World

CsharpServer Side ProgrammingProgramming

The following is a simple “Hello World” 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 HelloWorld.
  • The class MyDemoClass contains the data and method definitions that your program uses. Classes generally contain multiple methods.
  • The 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.
raja
Published on 24-Jul-2018 07:27:05
Advertisements