C# Comments

  • Single line comments: sample
  • // This is a single line comment
  • Multiple line comments: sample
  • /* This is multiple line comments
       Author: XXXXXX
       Date:   2014-07-01
    */
  • Comments are added to explain who created the code and what time was the code created, etc.
  • Comments are added to make the code readable or explain what the code means.
  • We can temporarily comment out unused code in the program by adding // and remove the double slash to get it back.

Example 01-04-01 was changed as follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;

namespace ValueTypesVSReferenceTypes
{
    class Person
    {
        public byte age;
    }

    class Program
    {
        /* Value type VS reference type
         * Author: Steven
         * Date:   2014-07-15
         */
        static void Main(string[] args)
        {
            /*int a = 1;
            int b = 2;
            b = a;          // a => b
            b = 3;          // 3 => b*/

            Person john = new Person();
            john.age = 10;
            Person mike = new Person();
            mike.age = 20;
            mike = john;    // mike and john point to the same object
            mike.age = 30;

            //Console.WriteLine("a={0}, b={1}", a, b);
            Console.WriteLine("john.age={0}, mike.age={1}", john.age, mike.age);
            Console.Read();
        }
    }
}

Output

john.age=30, mike.age=30
  • Line 12-15: Multi-line C# comments. Put author and coding code here.
  • Line 18-21: Code was commented out with a multi-line comment.
  • Line 30: Single line comment is used to comment out the variables a and b output.
  • Now only the 2 objects' ages are printed.
Note Note
Don't add space between the comment 2 characters. That means / *, * / or / / are all invalid comment start or end.