C Sharp Hello World

C# Example

There are multiple ways to write even a simple program like “Hello World” in C# programming language. We are discussing below only the most recommended four ways to write a simple program in C#.

  • Simple Example
  • Using System
  • Using public modifier
  • Using namespace

Example 1: C# Simple Example.

class Example 
    {  
        static void Main(string[] args)  
        {  
            System.Console.WriteLine("Hello World!!");  
        }  
    }

Output:

Hello World!!

Explanation:

  • class: It is a keyword. Used to define a class.
  • Example: It is the name of the class. A class in C# can be understood as a blueprint or template from which objects are created. In the above example, it has only the Main method however, it can have other data members and methods also.
  • static: It is a keyword. It is used to save memory because the object is not required to access the static members.
  • void: It is the return type of a method. It is used to return no values. Thus the return statement is not required in this case.
  • Main: It is the name of the Method. It is used as the entry point or a start-up for a C# program and is thus invoked first before any other method whenever a C# program runs.
  • string[] args: It is used for command-line arguments in C#. Arguments are the values that we can pass while running a C# program.
  • System.Console.WriteLine(“Hello World!”): The System is used to specify the namespace. The Console is used to specify the class defined in the System namespace. The WriteLine() is used as a static method of the Console class to write the text on the console.

Example 2: C# example using System.

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

Output:

Hello World!!

Explanation:

In the above example, we have written “using System” before the class. Thus the System namespace is not needed to be specified for accessing any class of this namespace. Also, in the above example, the Console class is used without specifying the System.Console.

Example 3: C# example using public modifier.

using System;  
    public class Example  
    {  
        public static void Main(string[] args)  
        {  
            Console.WriteLine("Hello World!!");  
        }  
    }

Output:

Hello World!!

Explanation:

In the above example, we are specifying the “public” modifier before the class and the Main() method. Thus, it can also be accessed from outside the class.

Example 4: C# example using namespace.

using System;  
namespace xyz  
{  
    public class Example  
    {  
        public static void Main(string[] args)  
        {  
            Console.WriteLine("Hello World!!");  
        }  
    }  
}

Output:

Hello World!!

Explanation:

In the above example, we are creating classes inside the namespace. A namespace in C# is used to group related classes or to categorize the classes, thus facilitating easy maintenance.

Please Share