C# Programming: My First C# Program

Getting Started: Create a new project

Let's learn how to start with C# Programming by creating your first C# Program.

Example: How to write a program that shows the output "Hello World !", "Welcome to C-Sharpcorner".
Open your Visual Studio 2010/2008/2005/2003, go to "File" -> "New" -> "Project...". Click on "Visual C#" from the Installed Templates and select "Windows".
Now select "Console Application" from the available list. Provide a name for your project then browse to the path location to save the project and click on "OK".

Refer to the following screen shot:

Once you have created the project, it will be opened and a file named "Program.cs" shown to you.
Write First line of code

Now just write the following code inside the Main block:

//using Write method
Console.Write("Hello World !");
Console.Write("Welcome to C-Sharpcorner");

Save the file and hit F5. You will see a black window suddenly opened and closed.
Due to that we are unable to see the actual printed output. To overcome this we need to introduce one more line.

Now again press F5, now the output is shown.

If you observe, the output is shown in a single line without a space. So how to show both sentence in separate lines or in the next row?
how to put a break between two sentences, use the following code to do it.
//using WriteLine and ReadKey method
Console.WriteLine("Hello World !");
Console.Write("Welcome to C-Sharpcorner !");

Now save the file and hit F5, the output is shown. DateTime.Now will show the system date and time.

Great, your first C# Project is ready.
Code Explained

Now check this attached image with the complete code. Let me try to explain each line of code, please follow the numbering labels.
  1. The "using" keyword. using is used to import the namespaces that specify the types referenced in the code. 
           For example: System is a namespace and Console is a class in that imported namespace (using System).
           These namespaces are provided by the .Net Framework so that we can reuse it every time.
    2.    namespace MyFirstProgram: Now here we are declaring our own or custom namespace with the class named Program.
           Creating our own namespace will help us to control the scope of class and methods it contains. It behaves like a container that holds classes and methods.
   3.    class Program: Class is like a blueprint that defines the state and behaviour of a type. A class helps to group methods, events and so on under one roof.
   4.    static void Main(string[] args)
          When we say static, then only one copy of the class Program exists in memory and can directly access through the class itself, no need to instantiate it.
          void is used as a return type for a method, and it specifies that the method Main does not return a value.
          Main is the entry point of every program where the program control starts and ends.
  5.     Console.Write

          Writes the given information to the standard output stream. In our case it writes "Hello World !" and "Welcome to C-Sharpcorner".
  6.    Console.WriteLine

         Writes the given information to the standard output stream followed by the current line terminator. Refer to image 3.
         Obtains the key pressed by the user or the next character and displayed in the output window.


That's it, your first C# program is created. The sample project is attached.
Wait for my next article. Post your queries to our Answer/Forum Module.
Thank you !