Close
Close

C# Printing and Commenting


In this chapter, you will learn about working of a C# code. After this chapter, you will also be able to write basics codes on your own. So, let's start.

Printing in C#


In the last chapter, we showed how to run a C# code on different platforms. We also wrote a code to print "Hello World" on the screen. But you still don't know how the code worked and printed "Hello World". So, let's start by explaining the previous code.

using System;

class Hello
{
static void Main(string[] args)
{
  Console.WriteLine("Hello World");
}
}
Output
Hello World

This code printed "Hello world" on the screen. Basically, we have written a code to print any message on the screen. Let's try this out with a different message.

using System;

class Print
{
static void Main(string[] args)
{
  Console.WriteLine("It's working!");
}
}
Output
It's working!

Yes, it worked, we just printed a different message on the screen. Now it's time to understand the working of the code by understanding each line of the code.

You won't be able to get the entire code thoroughly in this first chapter but gradually, with the progress with the course, you will do. This makes sense because programming is all about consistency and practice. However, you will have enough understanding after this chapter to start writing and understanding different codes.

using SystemSystem is a namespace and using is a keyword. It means that we are using the System namespace in our program. Now, let's discuss what is a namespace.

Basically, a namespace is like an area inside which something is defined. For example, we can define the value of pi as 3.14 in a namespace Constant and use it in our program with using Constant;. So, using is used to tell the compiler to use a particular namespace in our program.

namespace in C#

System namespace is predefined in C# but we can also make our own namespaces which we will study further in this course.

class Hello → 'Hello' is just the name of a class and full details about classes will be explained later. For now, just understand that we will write our code in this format i.e., we will first write class and then the name of the class - class Hello {} and then write our codes inside the curly braces.

There could be any other names of the class like FirstProgram, MyCode, etc.

{} → Curly braces '{}' following class Hello represents the body of the class. All the statements written inside this curly braces are in the body of the class Hello.

{} in C#

static void Main(string[] args) → Here, Main is the name of a method (you will learn about methods later). Its significance is that whenever we run our code, this Main method is executed first. Similar to a class, we have used '{}' with Main method also to represent its body.

Thus, static void Main(string[] args) is inside the class Hello and Console.WriteLine("It's working!"); is inside the Main method.

hello world in C#

Console.WriteLine("It's working!"); → It is a statement inside the Main method. As stated earlier, the Main method is executed first i.e., the statements written inside it (inside curly braces following Main method) will be executed first. So, the statement Console.WriteLine("It's working!"); got executed.

You already know that a namespace is like an area which contains some definitions and we are using System namespace in our program. Console is a class which is defined inside the System namespace and WriteLine is a method inside the Console class.

WriteLine in C#

A method is something which performs a specific task for example, we can have a method which adds two numbers, a method to find the difference between two numbers, etc.

method in C#

Similarly, WriteLine is a method which prints any message on the screen. So, we are using the System namespace inside which there is a Console class and this class has a method WriteLine which is used to print any message on the screen and thus, we printed "Hello World" using it.

writeline method in C#

In C#, we end our statements with a ';' (semicolon). Notice ';' after the end of Console.WriteLine("It's working!");. It means that our statement has ended there.

We can also write System.Console.WriteLine("Hello World") to use the WriteLine method inside Console class inside System namespace instead of writing using System; at the top of our code.

class Hello
{
static void Main(string[] args)
{
  System.Console.WriteLine("Hello World");
}
}
Output
Hello World

The takeaway is - methods are used to perform some specific tasks and we used the WriteLine method to print "Hello World" on the screen. To execute this method, we wrote this statement inside the Main function because the statements inside the Main function are executed first when a program is compiled. The WriteLine method is defined under the Console class and this class is defined inside System namespace. So, we included this namespace first in our program using using System;.

working of hello world program in C#

It is totally fine if you have not understood the entire code like we have not yet explained what is a class, what is static void written before Main and also (string[] args) written after Main. But the important thing is that you have the basic understanding of how this code is functioning and the basic format used to write codes.

Thus, the basic format to follow is:

class CLASSNAME
{
static void Main(string[] args)
{
  STATEMENT;
  STATEMENT;
  STATEMENT;
  ...
}
}

C# Special Characters


There are many special characters like '\n', '\r', '\t', etc. in C# but we are going to explain '\n' and '\t' in this chapter. You can always search for different special characters when you need them and a good web search is also an integral part of any professional programmer. You will also spend a lot of time searching web as a programmer.

\n


Let's try a different method to print messages on the screen i.e., Write instead of WriteLine.

using System;

class Hello
{
static void Main(string[] args)
{
  Console.Write("Hello World");
}
}
Output
Hello World

Run this code and you will find out that Write also printed "Hello World" on the screen but everything after it got printed in the same line. Actually, WriteLine adds a new line after printing the message but Write doesn't and this is the difference between these two methods.

difference between Write and WriteLine in C#

However, we can use \n (newline character) to print a new line. It is like English characters 'a', 'b', 'c', etc. but prints a new line. Let's look at some examples.

using System;

class Hello
{
static void Main(string[] args)
{
  Console.Write("Hello World\n");
}
}
Output
Hello World
using System;

class Hello
{
static void Main(string[] args)
{
  Console.Write("Hello\nWorld\n");
}
}
Output
Hello
World
using System;

class Hello
{
static void Main(string[] args)
{
  Console.Write("\nHello\nWorld\n");
}
}
Output

Hello
World

\t


\t is a tab character and is used to insert tab spaces. Let's look at the example given below.

using System;

class Hello
{
static void Main(string[] args)
{
  Console.WriteLine("Hello\tWorld");
}
}
Output
Hello   World

As you can see, '\t' inserted one tab space between "Hello" and "World".

C# Commenting


Comments are statements written inside our code which are just ignored by the compiler while compiling our code. Comments are for humans to read and not for computers. These are written to make our code more readable. Comments are written between /* */ or after // or ///.

Let's look at an example.

using System;

/*
  This is a comment in between
  the code.

  We can use this comment to
  explain other programmers reading this
  code that this is a Hello World
  code
*/

class Hello
{
static void Main(string[] args)
{
  Console.WriteLine("Hello World");
}
}
Output
Hello World

You can see that everything written between '/* */' is ignored by the compiler and one can read it to get the better understanding of the code.

Why comments?


As mentioned earlier, it makes our code more readable. Assume that you have written a software and after releasing it, you hired a few good programmers for the maintenance. Without comments, it would be a very difficult task for them to understand your code. And most of the time it happens that the person who has written a code is not the one who is going to modify it. So, make it a habit of writing comments.

C# Comments after // (Single Line Comments)


Comments written after // are single line comments. Thus if you change the line, then the new line will not be a part of your comment.

using System;

//This is single line comment
//This is also a comment

class Hello
{
static void Main(string[] args)
{
  Console.WriteLine("Hello World");
}
}
Output
Hello World

C# Comments between /**/ (Multi Line Comments)


Comment can also be multi-lined by enclosing it between /* and */ as shown in the following example. But we can't put one comment inside another. For example, /* This is a /*comment*/ */ is invalid because it has one comment inside another.

using System;

/*Hello World code*/
/* Comments will not be compiled and will be ignored */
/* It is a
multiline
comment*/

class Hello
{
static void Main(string[] args)
{
  Console.WriteLine("Hello World");
}
}
Output
Hello World

C# Comments after /// (XML Documentation Comments)


Comments written after /// are also single-line comments and are used to create documentation of the code using XML elements.

using System;

/// <summary>
/// This is a XML Comment
/// </summary>

class Hello
{
static void Main(string[] args)
{
  Console.WriteLine("Hello World");
}
}
Output
Hello World

For a beginner, one can ignore the XML documentation comments and can use only the first two.

In future, while writing codes, make sure that you run your code from time to time during writing instead of completing your whole code and running at last. This will make debugging your code easier if you have made any error.

Ask Yours
Post Yours
Doubt? Ask question