Using comments in C#

Comments are useful for explaining the purpose of a piece of code or how it works. Comments can also be used to identify the author of code or when it was created or last updated.

In C# the // characters (two forward slashes) are used to add comments to your code. Any text on a line after the two forward slashes will not be treated as code. Comments are useful as they contain notes about what is happening in the code and can help others understand the algorithm. They won’t change the way a program runs and won’t be visible in the working program.

Here is an example of a comment:

// This is a comment

Watch the video below to see how to use comments and scroll down for the sample code.

Sample code

using System;

namespace MyCSharpProject
  class MainClass
    public static void Main(string[] args)
      string message = "The result is ";
      float num1 = 6f; // Assign value to num1 variable
      float num2 = 4f; // Assign value to num2 variable

      float result = num1 + num2 + 10f; // Add numbers

      result = num1 - num2; // Subtract
      result = num1 * num2; // Multiply
      result = num1 / num2; // Divide
      result = num1 % num2; // Mod

      int num3 = 30;


Next tutorial: Combining types in output statements