Hello world C# Example

Hello world C# program- Creating Hello world C# program with steps to create project, namespace and class in Visual studio editor with important points explained.

 

Steps to Create a first Hello World program in C# in Visual studio editor

  • On top menu, Click File -> New -> Project
  • On New project window – > select visual C# template on left side ->select console application ->
    Enter a project name whatever you want. For example, C Sharp Course, -> Click OK, you should see below code.

 

Hello world program Example in C#

using System;

namespace helloworldExample
{
    class Program
    {
        static void Main(string[] args)
        {
           // TODO Auto-generated method stub
        }
    }
}

 

NOTE: // TODO Auto-generated method stub” in above program is just a comment that you can remove.

You can write Console.WriteLine(“Hello World”); in main() method as below.”

 

 

using System;
namespace helloworldExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World");           
            
        }
    }
}

Save the file and run the program by clicking start button on top menu. Hello world string will be printed on console screen. for code debugging purpose press ctrl+F10.

Here is the output of above program

Output:
Hello World

 

Now you learned how to create a hello world program in C# programming. Focus on special notes given below.

Special Notes:

  • It is mandatory to have main() method to run an application/program. Because compiler find main method first to execute a program. If you don’t have the main() method, then compiler will complain that ‘helloworldExample.exe’ does not contain a static ‘Main’ method suitable for an entry point.

 

NOTE:

public class: class is public, so it can be accessible from anywhere in the project.

static: method can be called using class name without creating object of the class.

void: method does not return anything.

Main: method is the starting point of program execution.

 

Written By Shilpa Jaroli & Reviewed by Rakesh Singh



Leave a Comment