A comment is an annotation in a source code with the intention to give a programmer a readable explanation of the code. These annotations are ignored by compilers when compiling your code. Comments should explain, at a higher level of abstraction than the code, what you're trying to do.
C# has different syntax for comments. In this guide, we’ll look at single-line and multi-line comments.
Single-line comments allow narrative on only one line at a time. Single-line comments can begin in any column of a given line and end at a new line or carriage return.
The //
character sequence marks the text following it as a single-line comment. Here’s an example
1class Account
2{
3 private string _accountNo;
4
5 public Account(string accountNo, int balance)
6 {
7 AccountNo = accountNo;
8 Balance = balance;
9 }
10
11 public string AccountNo
12 {
13 get => _accountNo;
14 set
15 {
16 //raise exception if value is null
17 if(String.IsNullOrEmpty(value))
18 throw new InvalidOperationException();
19 _accountNo = value;
20 }
21 }
22
23 public int Balance { get; set; }
24
25 public void Credit(int amount)
26 {
27 Balance = amount + Balance; //update the balance with a summation of the inputted amount and the current balance
28 //Logs the new balance to the screen
29 Console.WriteLine(Balance);
30 }
31}
The code above uses single-line comments within the Credit
method and the set accessor for the AccountNo
property.
Multi-line comments have one or more lines of narrative within a set of comment delimiters. The /*
delimiter marks the beginning of the comment, and the */
marks the end. You can have your comment span multiple lines and anything between those delimiters is considered a comment
1class Account
2{
3 /* Account class constructor.
4 In it we initialise the AccountNo and Balance property.
5 These properties are needed to identify an account and perform transaction using its available methods
6 */
7 public Account(int accountNo, int balance)
8 {
9 AccountNo = accountNo;
10 Balance = balance;
11 }
12
13 public int AccountNo { get; set; }
14
15 public int Balance { get; set; }
16
17 public void Credit(int amount)
18 {
19 Balance = amount + Balance;
20 }
21}
It can also be placed inside code statements.
1var account = new Account(2 /* this is a dummy account number */, 2000);
The code above tells the reader that the number 2
is just a fake account number that should be replaced later. Be careful with the use of comments. If a code block is hard to read, endeavour to re-write to a more readable form.
Code comments can be extremely useful in helping you, or other programmers who will use your code, to understand your code better. It is a way of annotating source code with helpful narratives. Over-use it and you get source code that’s hard to read and understand.
We looked at single-line and multi-line comment in C#. Single-line comments end at the first end-of-line following the //
comment marker. You can place it at the top of the code statement or after the code statement. If it’s after a code statement, whatever text following it is regarded as the comment. On the other hand, multi-line comments can span many lines or be placed within a code statement and only the content between the comment delimiters will be treated as the comment and ignored during compilation.