C# Syntax, Write() & Comments

C# Syntax

The following code was used to print “Hello World” on the screen in the C# file Program.cs that we generated in the previous chapter:

using System;


namespace HelloWorld

{

  class Program

  {

    static void Main(string[] args)

    {

      Console.WriteLine(“Hello World!”);    

    }

  }

}

Described with an example
Line 1: Classes from the System namespace can be used when we utilize System.

Line 2: The second line is empty. White space is ignored by C#. Still, the code is easier to read when it has more lines.

Line3: Namespaces are used to group together classes and other namespaces in your code.

Line 4: A block of code is indicated by the curly brackets {} at the start and end.

Line 5: Your application has functionality since the class serves as a container for data and methods. All C# code must be contained within classes. The class we used in our example was called Program.

If you’re not sure how to use System, namespace, or class, don’t worry. Consider it simply as something that (almost) always comes up in your program; you will get more information about it in a later chapter.

Line 7: The Main method is something else that is a given in a C# program. The code included in curly brackets {} will be run. The terms that come before and after Main are not necessary to comprehend. Reading this tutorial will allow you to gradually get to know them.

Line 9: To output or print text, use the WriteLine() function of the Console class, which is a member of the System namespace. “Hello World!” will be the output in our case.

You would have to write System.Console if the using System line was removed.To print or output text, use WriteLine().

Note: A semicolon ; completes each and every C# statement.


Note that “MyClass” and “myclass” have different meanings in C# due to case sensitivity.


Note: Unlike Java, the class name and file name in C# do not always have to match, but they usually do (for easier organization). Put “.cs” at the end of the filename and save the file with a proper name. Make sure C# is installed correctly on your computer before attempting to execute the preceding example: To learn how to install C#, refer to the Get Started Chapter. The result ought to be:

Hello World!

C# Output

You can use the WriteLine() function in C# to output values or print text:

Example

Console.WriteLine(“Hello World!”);

You are able to add an unlimited number of WriteLine() methods. It should be noted that every procedure will create a new line:

 Console.WriteLine(“Hello World!”);

      Console.WriteLine(“I am Learning C#”);

      Console.WriteLine(“It is awesome!”);

 

Additionally, you can compute mathematical expressions and output numbers:

      Console.WriteLine(3 + 3);

The Write Method

Write() is another method that is comparable to WriteLine().


The sole distinction is that the output ends without a new line being inserted:

      Console.Write(“Hello World! “);

      Console.Write(“I will print on the same line.”);

Note that we add an extra space where needed (after “Hello World!” in the example above), for better reading.

Since WriteLine() facilitates easier code reading, we shall solely use it in this lesson.

C# Comments

C# code can be made more readable and understandable by adding comments. In testing alternative code, it can also be used to stop execution.

Single-line Comments


Two forward slashes (//) are the beginning of a single-line comment.


C# ignores (does not execute) any text between // and the end of the line.


A single line of commentary is used before a line of code in this example:

Example

// This is a comment

      Console.WriteLine(“Hello World!”);  

In this example, a line of code ends with a single-line comment:

Example

Console.WriteLine(“Hello World!”);  // This is a comment

C# Multi-line Comments

Comments with multiple lines begin with /* and conclude with */.


C# will not recognize any content that is between /* and */.


In order to illustrate the code, this example employs a multi-line remark, or comment block:

 /* The code below will print the words Hello World to the screen, and it is amazing */

 Console.WriteLine(“Hello World!”);

Single or multi-line comments?

Whichever you wish to use is up to you. Generally, we use /* */ for larger comments and // for shorter ones.