C#

Comments In c# net | trickcode

Comments in C#,comments, multiline comments ,single-line comments
Share it:

Comments in C#



When writing code, you might want to add some text that will serve as a reminder or a note for you or for anyone who will read your code. In C# (and most programming languages), you can do that using comments. Comments are ignored by the compiler and are not part of the actual C# code. Their main purpose is to make it easy for you or to anyone who reads your code to determine the role of your code inside your program. Suppose you want to describe what a particular code is going to do, then you can place a comment above or beside it. It is also used for documentation purposes. Here is an example of a program with a comment:


namespace CommentsDemo
{
    class Program
    {
        public static void Main(string[] args)
        {
            // This line will print the message hello world
            System.Console.WriteLine("Hello World!");
        }
    }
}

Shows an example of a single-line comment. There are two types of comments, single-line comments and multiline comments as presented below:


// single line comment
 
/* multi 
 line
 comment */



Single line comments as the name implies, are comments good for one line only. A single-line comment starts with // and everything to its right will be part of the comment. the Single-line comments are often placed above or to the right of a single line of code. These comments are ideal for describing the functionality of a single line of code.

If the comment is longer and requires multiple-lines, use the multiline comment. A multiline comment starts with /* and ends with */. Everything between /* and */ will be considered comments. the type of comment is useful for adding details about the program at the source code's header or any long comments that spans multiple lines.

They are another type of comment which is called XML comment. It is represented by three slashes (///). It typically functions as a single line comment but it is commonly used for creating automatic documentation for your code. You will learn more about XML comments and how to use them in a separate lesson.


Share it:

C#

Post A Comment:

0 comments: