In the previous section, we have created our first C#  console project. Now, let’s write simple C# code to understand important syntax and building blocks.

First C# Program

Every console application starts from the Main() method of Program class. The following example code displays “Hello World!!” on the console.

using System;

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

Line 1: using System means that we can use classes from the System namespace.

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

Line 3: namespace is a used to organize your code, and it is a container for classes and other namespaces.

Line 4: The curly braces {} marks the beginning and the end of a block of code.

Line 5: class 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.

Don’t worry if you don’t understand how using Systemnamespace and class works. Just think of it as something that (almost) always appears in your program, and that you will learn more about them in a later chapter.

Line 7: Another thing that always appear in a C# program, is the Main method. Any code inside its curly brackets {} will be executed. You don’t have to understand the keywords before and after Main. You will get to know them bit by bit while reading this tutorial.

Line 9: 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!”.

Line 10: ReadLine() is a methode used to read the Next line from console. This methode allows us to keep the console open after it shows the message “Hello World”, otherwise console will close very fast and you won’t be able to read the results after the program executes.

If you omit the using System line, you would have to write System.Console.WriteLine() to print/output text.

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

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

 

WriteLine or Write

The most common method to output something in C# is WriteLine(), but you can also use Write().

The difference is that WriteLine() prints the output on a new line each time, while Write() prints on the same line (note that you should remember to add spaces when needed, for better readability):

using System;

namespace HelloWorld
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Hello World!");  
      Console.WriteLine("I will print on a new line.");

      Console.Write("Hello World! ");
      Console.Write("I will print on the same line.");   
      Console.ReadLine();
    }
  }
}

Result:

Hello World!
I will print on a new line.
Hello World! I will print on the same line.

 

C# Comments

Comments can be used to explain C# code, and to make it more readable. It can also be used to prevent execution when testing alternative code.

Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by C# (will not be executed).

This example uses a single-line comment before a line of code:

using System;

namespace HelloWorld
{
  class Program
  {
    static void Main(string[] args)
    {
      // This is a comment
      Console.WriteLine("Hello World!");  
      Console.ReadLine();
    }
  }
}

This example uses a single-line comment at the end of a line of code:

using System;

namespace HelloWorld
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Hello World!");  // This is a comment 
      Console.ReadLine();
    }
  }
}

 

C# Multi-line Comments

Multi-line comments start with /* and ends with */. Any text between /* and */ will be ignored by C#.

This example uses a multi-line comment (a comment block) to explain the code:

using System;

namespace HelloWorld
{
  class Program
  {
    static void Main(string[] args)
    {
      /* The code below will print the words Hello World
      to the screen, and it is amazing */
      Console.WriteLine("Hello World!");  
      Console.ReadLine();
    }
  }
}

Sources : 

https://www.tutorialsteacher.com/csharp/csharp-tutorials

https://www.w3schools.com/cs/default.asp