C# Hello World Program

In the C# programming language, a "Hello, world!" program is a simple program that prints only "Hello, world!". This program is commonly used to introduce beginners to a new programming language. With this simple program, beginners can understand how the C# programming language works. The Hello World program can be written in multiple ways. Let's see the top 4 ways to create a simple C# example:

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

Here, we will discuss these four ways to print Hello World one by one.

C# Simple Example

Let us take an example to print Hello World in a simple way.

Example

Example

class C# Tutorial  

    {  

        static void Main(string[] args)  

        {  

            System.Console.WriteLine("Hello World!");  

        }  

    }

Output:

Output

Hello World!

Working of C# Program

There are several main concepts that need to be learn while writing a C# program. By using the above program, we will understand the working of a C# program.

  • class: It is used to represent a keyword that is used to define a class.
  • ClassName (Program) : It is used to represent the name of the class. A class is a blueprint or template from which objects are created. It can have data members and methods.
  • static: It is a keyword that means that the object is not required to access static members. Therefore, it saves memory.
  • void: It is the return type of the method. It does not return any value. In such a case, a return statement is not required.
  • Main: It is used to represent the method name. It is the entry point for any C# program. Whenever we run the C# program, the Main method is called first before any other method. It represents the startup of the program.
  • string args: It is used for command-line arguments in the C# programming language. While running the C# program, we can pass values. These values are known as arguments that we can use in the program.
  • Console.WriteLine("Hello World!"): Here, System is used to represent the namespace, Console is the class defined in the System namespace, and the WriteLine is the static method of the Console class that is used to write the text on the console.
  • C# Hello World Program Using System

In the C# programming language, we can use the using directive that enables us to access any class inside a namespace by defining its complete qualified name. Namespace for accessing any class of this namespace. If we use the "using System;" at the start of the program, we can use the Console class without specifying System.Console.

Let's take an example to print Hello World by using System in C#.

Example

Example

using System;  

    class C# Tutorial  

    {  

        static void Main(string[] args)  

        {  

            Console.WriteLine("Hello World!");  

        }  

    }

Output:

Output

Hello World!

Explanation:

In this example, we have taken the using System; directive that helps to access the Console class directly without specifying System.Console. In the Main method, we print "Hello World!" on the console using the Console.Writeline function.

C# Hello World Example Using public modifier

In the C# language, we can also specify the public modifier before the class and the Main method. It can be accessed from outside the class, and make sure that the program's entry point is visible to the runtime.

Let us take an example to print Hello World by using the public modifier in C#.

Example

Example

using System;  

    public class C# Tutorial  

    {  

        public static void Main(string[] args)  

        {  

            Console.WriteLine("Hello World!");  

        }  

    }

Output:

Output

Hello World!

Explanation:

In this example, we have taken both the C# Tutorial class and the Main method as public. It means that they can be accessed from outside the class, which is very helpful for larger applications.

C# Hello World Example Using namespace

In the C# programming language, we can create classes inside a namespace to group related classes together. It is used to categorize classes, organize code, and avoid naming conflicts, which helps to make the program easier to maintain and understand.

Let us take an example to print Hello World using a namespace in C#.

Example

Example

using System;  

namespace ConsoleApplication1  

{  

    public class C# Tutorial  

    {  

        public static void Main(string[] args)  

        {  

            Console.WriteLine("Hello World!");  

        }  

    }  

}

Output:

Output

Hello World!

Explanation:

In this example, we have taken a class C# Tutorial that is defined inside the namespace ConsoleApplication1. After that, we declare the Main method as public and show the "Hello World!" using the Console.WriteLine function.

Conclusion

In conclusion, the Hello World Program in C# is a simple example that helps beginners to get started with the C# language. In this article, we have covered the basic structure of a C# program, such as namespaces, classes , methods, and Console.WriteLine method is used to print the output.

Hello World Program FAQs

1) What is the Hello World Program in the C# programming language?

The Hello World program is the simplest and basic program of the C# programming language. It helps beginners to learn the basic syntax and structure of the C# program.

2) Can we write a Hello World program without using the namespace method in C#?

Yes, because namespaces are optional in the Hello World Program. We can directly write the Hello World program with a class and the Main method. Namespaces are commonly utilized for better organization in large projects.

3) Why do we use the "using System;" in the C# program?

In the C# programming language, we use the "using System;" to access classes in the System namespace without specifying the full name System.Console.

4) What is the main difference between public and default class access in C#?

The main difference between public and default class access in C# is that a public class can be accessed from outside its own class and assembly. On the other hand, the default class can only be accessed inside the same assembly.

5) What is the use of the Main method in C#?

In the C# programming language, the Main method is the entry point of the program. The program starts its execution from this method. It can be declared as static, and also contains optional parameters, including string args.

Input Required

This code uses input(). Please provide values below: