C# Hello World

C# Hello World

Before we study basic building blocks of the C# programming language, let us look at a bare minimum C# program structure so that we can take it as a reference in upcoming chapters.

in this tutorial, you’ll learn how to create a simple program that displays the famous message Hello, World! on a console window.

 

Creating the C# hello world program

In order to create a C# console based application

  • Open your Visual Studio
  • On the File menu, click New Project. (Then the New Project dialog box appears. This dialog box lists the different default application types.)
Creating the C# hello world program
  • Select Console Application as your project type and change the name of your application at the bottom textbox. (If you are not comfortable with default location, you can always enter a new path if you want.)
C# Console Application
  • Then Click OK.

After click OK button, you will get a screen like the following picture:

Creating the C# hello world program

If you observe the above image, by default the application contains a Main() method because the console applications in the c# programming language will always start from the Main() method of the Program class.

Here in the following program, we just print a "Hello, world!" message only. So copy and paste the following command in the main code block.

Console.WriteLine("Hello, world!");
Console.ReadLine();

Here you can see the full source code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, world!");
            Console.ReadLine();
        }
    }
}

After enter the command, the next step is to run the program. You can run your program using Ctrl+F5 . Then Visual Studio will keep the console window open, until you press a key. You will get screen look like the following picture:

Console.WriteLine("Hello, world!");

Now you created your first program in Visual Studio.

 

What do these statements do?

We will go through each step of our c# program and learn each parameter in a detailed manner. 

  • The using keyword is used to include the System namespace in the program. A program generally has multiple using statements.
  • namespace is used to organize your code, and it is a container for classes and other namespaces.
  • class Program is a container for data and methods, which brings functionality to your program. Every line of code that runs in C# must be inside a class. In our example, we named the class Program.
  • The curly braces {} marks the beginning and the end of a block of code.
  • The Main() method is the entry point of our console application. Any code inside its curly brackets {} will be executed.
  • Console is a class of the System namespace, which has a WriteLine() method that is used to output/print text. In our example it will output "Hello World!".
  • Console.WriteLine outputs (writes) a single line to the user.
  • Console.ReadLine reads a line of text that the user enters with the keyboard.

Note: A blank line. C# ignores white space. However, multiple lines makes the code more readable.

Note: Every C# statement ends with a semicolon ;.

Note: C# is case-sensitive: "MyClass" and "myclass" has different meaning.