C# Hello World

C sharp is a programming language used to create and execute the program we do in other programming languages, including this ‘Hello World’. In C sharp programming language, the most basic code for the execution is the “Hello World” program. It prints the sentence on the console, the output screen. The basic C sharp source code program contains some features. These are described as:

  • Declaration of Library.
  • Using a namespace.
  • Declaration and definition of the class.
  • Members of the class, including methods, variables, etc.
  • Main method.
  • Statements you want to print or the expressions.

We will consider a simple example of the “Hello World” source code.

The output of the above program is:


Using system

A system is a library used to add some commonly used types. This namespace is specified to be declared with the ‘using’ directive of the system.

Namespace <name>

It is a user-defined name that is given to the namespace. It is the user’s choice to provide any name to the namespace. The purpose of namespace is that it is used to handle and organize the classes. The namespace’s name is followed by the curly bracket containing the namespace body.

Class sample

The class is the keyword used to declare classes in the program. For example, here sample is the name given to it. Like namespace, the name of the class can be any word.

Static void main (string[] args)

While defining the main program, we use the word static with it. The static keyword shows that the main program can be accessed anywhere without creating an object or an instance. The second keyword is void; it shows that the return type of this program is zero; in other words, this void keyword shows that the main program does not return any value. It specifies the main method and its relation with the expressions used inside it, especially with the statement Console.Writeline(“Hello World!”);


The writeLine() is a built-in method that is declared in the console class and is defined in the System namespace. That is the reason for using the system class library in our source codes of C sharp.


Like the WriteLine() function, this is also a built-in method. It takes the input entered at the console and lets it be stored in the variable to the program. This allows the program to wait until any key is pressed, and it also prevents the console screen from closing and running quickly.

Execute a C Sharp Program

To run the source code of C sharp, there are several ways to be followed. But the basic two approaches that are used are described below.

Use a visual Studio

A visual studio is a tool developed to execute programs in different languages like Visual basics, C++, C sharp, etc. We need to buy a license for the commercial use to install and then configure the visual studio in our system. For non-commercial purposes, Microsoft gives authority to the usage of Visual Studio Community Version. The visual studio also has a process to write a new code, compile it and execute it. But one advantage it has over the local compiler is that the built-in compilers compile and execute the code without specifying any compiler.


The second and most commonly used method is the Command-Line option. It uses some basic steps to be followed. We have also used this approach as it is highly recommended and can be applied easily. As we have implemented the code in Linux so, go to the applications of Ubuntu.

First, go to the menu, and open the text editor. Write the source code in the editor and then save the file at any location with the extension of .cs. We use the Linux terminal and MCS compiler to execute and see the result. Add the file name along with the compiler.

If the program has no exception, error, or warnings on the compilation, it will create an executable file file.exe in the same folder where we have saved the file.cs. For this file.exe, we use Mono to execute the file.

By following the above steps, you will see that the console’s statement ‘hello world!’ is displayed.

Another way of declaring the same program is that if you forgot to use the name of the library ‘system’ at the start, you could also use the system namespace and the console statement.

System. Console. WriteLine (“Hello World!”);

On execution, you will see that the statement is displayed on the console.

Printing on Console

C sharp contains two basic methods to display output on the console and get the console’s input, as we have declared one of them as writeline(). It is used to display the value.


It is an output function. It is the built-in method used to display all the contents on a single line on the console. But if you want to display each word on a separate line, we use the ‘WriteLine()’ method.


Similarly, this is the function used to get the value for the console that the user enters. It is an input function.

Now we will elaborate on this approach in the example. Using the same namespaces and the main program, we will use a string to store the input value from the user. Then through the writeline, the user will be asked to enter a value.

Console.writeline(“Enter a string – “);

After that, the user will enter a value stored in the string taken at the start.

Teststring = Console.ReadLine();

Using the writeline() function again, we will display the value entered and stored by the user.

On execution, you can see the results.

Similarly, if we take two strings separately for each word in the string, then with the help of readline, each word will be entered on separate lines, just like the method writeline().

But to display the string on a single line, we will use a simple write() method.

On execution, you can see that both separately entered values can be combined through the write() method.


‘Hello, World!’ is mostly the first program in every programming language. Similarly, C sharp also has this program as its introductory source code. A simple program in C sharp contains many important features; a program can be easily executable. All of them are explained in this tutorial with an elementary example to demonstrate the working of the C sharp program.

The string ‘Hello World!’ has become a benchmark for the introduction statement in the field of programming. But it is not mandatory to always use the same string. We can also alter this string by adding alpha-numeric characters and symbols.

Source link

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button