24
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
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
class Account { private string _accountNo; public Account(string accountNo, int balance) { AccountNo = accountNo; Balance = balance; } public string AccountNo { get => _accountNo; set { //raise exception if value is null if(String.IsNullOrEmpty(value)) throw new InvalidOperationException(); _accountNo = value; } } public int Balance { get; set; } public void Credit(int amount) { Balance = amount + Balance; //update the balance with a summation of the inputted amount and the current balance //Logs the new balance to the screen Console.WriteLine(Balance); } }
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
class Account { /* Account class constructor. In it we initialise the AccountNo and Balance property. These properties are needed to identify an account and perform transaction using its available methods */ public Account(int accountNo, int balance) { AccountNo = accountNo; Balance = balance; } public int AccountNo { get; set; } public int Balance { get; set; } public void Credit(int amount) { Balance = amount + Balance; } }
It can also be placed inside code statements.
1
var 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.
24