|
| 1 | +--- |
| 2 | +title: "Comments" |
| 3 | +--- |
| 4 | + |
| 5 | +A program's source code contains instructions for the computer to perform. However, this code is written and maintained by people. It is often useful to be able to place comments in the code to help someone reading that code understand how it works or what it is trying to achieve. This text is not something to be translated into machine code, it is for us humans. |
| 6 | + |
| 7 | +Programming languages support the ability to embed *comments* into the source code that are then ignored by the compiler. |
| 8 | + |
| 9 | +## In C# |
| 10 | +As stated, comments allow you to embed documentation and explanatory text within your program’s code. The comments are skipped by the compiler, so they have no affect on the program’s machine code. You write comments to help yourself and other people understand what you intend the program to do, and any thoughts you want to record along with the code. |
| 11 | + |
| 12 | + |
| 13 | +:::tip[Syntax] |
| 14 | + |
| 15 | +<a href="#FigureCommentsSyntax">Figure 5.13</a> shows the syntax for comments in C#. There are two types of comments, single line comments, and block comments that span multiple lines. |
| 16 | + |
| 17 | +- **Block comments** begin with `/*` and end with `*/` and contain any text in between these, including new lines. |
| 18 | +- **Single line comment** begin with `//` and end with a new line. |
| 19 | + |
| 20 | + |
| 21 | +<a id="FigureCommentsSyntax"></a> |
| 22 | + |
| 23 | + |
| 24 | +<div class="caption"><span class="caption-figure-nbr">Figure 5.13: </span>The syntax of comments</div><br/> |
| 25 | + |
| 26 | +::: |
| 27 | + |
| 28 | +## Example |
| 29 | + |
| 30 | +Example comments are shown within a code block in <a href="#ListingExampleComments">Listing 5.11</a> below</div> |
| 31 | + |
| 32 | +<a id="ListingExampleComments"></a> |
| 33 | + |
| 34 | +```csharp |
| 35 | +/* |
| 36 | + * This program calculates the area of a circle based on the radius. |
| 37 | + * Variables: |
| 38 | + * - the radius as a double |
| 39 | + * - the area as a double |
| 40 | + * - a line string to read text from the user |
| 41 | + */ |
| 42 | + |
| 43 | +using static System.Convert; |
| 44 | +using static System.Console; |
| 45 | + |
| 46 | +// Setup constants and variables |
| 47 | +const double PI = 3.1415; |
| 48 | +string line; |
| 49 | +double radius, area; |
| 50 | + |
| 51 | +// Get data from the user |
| 52 | +Write("Radius: "); |
| 53 | +line = ReadLine(); |
| 54 | +radius = ToDouble(line); |
| 55 | + |
| 56 | +// Calculate area |
| 57 | +area = PI * radius * radius; |
| 58 | + |
| 59 | +// Output area |
| 60 | +WriteLine(area); |
| 61 | +``` |
| 62 | + |
| 63 | +<div class="caption"><span class="caption-figure-nbr">Listing 5.11: </span>Example C# comments</div> |
| 64 | + |
| 65 | + |
| 66 | +## Activities |
| 67 | + |
| 68 | +1. Is the following a valid comment in C#? Why/why not? |
| 69 | + |
| 70 | +```csharp |
| 71 | +/* |
| 72 | + /* my comment */ |
| 73 | +*/ |
| 74 | +``` |
| 75 | + |
| 76 | +<div class="caption"><span class="caption-figure-nbr">Listing 5.12: </span>Sample code</div> |
| 77 | + |
| 78 | +:::note[Summary] |
| 79 | + |
| 80 | +- It is good practice to place a comment at the top of your code explaining what the program does. |
| 81 | +- Comments should be included to help other people read your code. You will also find these comments useful when you return to your code after a long break. |
| 82 | +- Make your comments meaningful by trying to capture your intentions and ideas |
| 83 | +- Comments have no impact on the output produced by the compiler. |
| 84 | + |
| 85 | +::: |
0 commit comments