c sharp Logo

C# Comments


Comments are non-executable code that is used to document a program. Comments are ignored by the compiler and are not part of the executable code. Comments are used to make code more readable and to explain what the code is doing.

There are two types of comments in C#: single-line comments and multi-line comments.

Single-line comments start with two forward slashes (//) and end with the end of the line.

// This is a single-line comment.

 

Multi-line comments start with a slash and an asterisk (/) and end with an asterisk and a slash (/).

/*

This is a multi-line comment.

It can span multiple lines.

*/

 

Comments can be used to document various aspects of a program, such as:

  • The purpose of a code block
  • The logic of a code block
  • The parameters of a method
  • The return value of a method
  • The variables used in a code block
  • The errors that can occur in a code block

Comments are an important part of any C# program. They make the code more readable and easier to understand, which can help other programmers maintain and modify the code.

Here are some examples of how to use comments in C#:

  • Documenting the purpose of a code block:

 

// This code block calculates the average of three numbers.

  • Documenting the logic of a code block:

 

// This code block checks if a number is even or odd.

if (number % 2 == 0)

{

    // The number is even.

    Console.WriteLine("The number is even.");

}

else

{

    // The number is odd.

    Console.WriteLine("The number is odd.");

}

  • Documenting the parameters of a method:

 

/// <summary>

/// Calculates the sum of two numbers.

 

///

 </summary>

 

///

 <param name="a">The first number.</param>

 

///

 <param name="b">The second number.</param>

 

///

 <returns>The sum of the two numbers.</returns>

 

public int Sum(int a, int b)

{

    return a + b;

}

  • Documenting the return value of a method:

 

/// <summary>

/// Returns the length of a string.

/// </summary>

/// <param name="str">The string to measure.</param>

/// <returns>The length of the string.</returns>

public int Length(string str)

{

    return str.Length;

}

  • Documenting the variables used in a code block:

 

// Declare two variables.

int number1, number2;

 

// Get input from the user.

number1 = int.Parse(Console.ReadLine());

number2 = int.Parse(Console.ReadLine());

 

// Calculate the sum of the two numbers.

int sum = number1 + number2;

 

// Print the sum to the console.

Console.WriteLine(sum);

  • Documenting the errors that can occur in a code block:

 

// This code block attempts to open a file.

// If the file does not exist, an exception will be thrown.

try

{

    StreamReader reader = new StreamReader("myfile.txt");

    // Read the file contents.

    string contents = reader.ReadToEnd();

    reader.Close();

    Console.WriteLine(contents);

}

catch (FileNotFoundException e)

{

    // The file does not exist.

    Console.WriteLine("The file does not exist.");

}