C# code can contain comments. A comment is text that is ignored by the computer on execution. You can add any kind of text inside your code. Comments can be single line or multi line.

Comments are used to aid developers. It can explain what a block of code does. Sometimes copyright headers are added to code or the name of an algorithm.

Quick Start

Comments in C#

Write some code. In this example we output some text. Then add single and multi line comments.

A single line comment starts with //. A multi line comment starts with /* and ends with */.

Copy the code below and save the file as hello.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;

namespace HelloWorldApplication {
class HelloWorld {
static void Main(string[] args) {
/* First C# program
This is a multiline comment */
Console.WriteLine("Hello, World");

// Single line comment
Console.ReadKey();
}
}
}

This code outputs

Hello, World

All the comments are ignored by the C# compiler.

Exercises

  1. Create a program and add a comment with your name
  2. Where can comments be placed in the code?