C# XML Docs
Describe Add with documentation comments, then run the same sum you already trust. Inside Pine, write public static int Add(int x, int y) so the body returns x + y. Immediately above that method, write four /// lines: one <summary>, one <param name="x">, one <param name="y">, and one <returns>. In Main, keep int x = 10; and int y = 2;, print both labels, and print Add(x, y). Run with dotnet run. The screen should show x=10, then y=2, then 12. Leave naming rules for the next page. This lesson is the documentation comment above a method that already adds.
Quality and Wrap-up continues, so the file stays Pine and the numbers stay the pair from the debugging page. That page printed x=10, then y=2, then 12 once the sum used plus. If the open file still says int sum = x - y, the last line is 8. Put the plus back, or call Add, before you judge this lesson. The /// lines do not turn 8 into 12. The plus does. After the operator is right, move that plus into Add and leave the labels in Main, so the screen you already know stays visible while the comment sits above the method.
int, return, and Console.WriteLine are already in the course. A line that starts with // is the ordinary note from the comments page. A documentation line starts with three slashes, ///, and carries an XML tag. Those tags are for a reader and for a documentation tool. They are not statements Main executes.
Three slashes above Add
Open the Pine file. The class name stays Pine. Keep using System; at the top. Delete leftover Machine, Truck, and Car types if they bury Add, leftover Equals calls, and any ReadLine line. The program must not wait for typing.
The documentation lines must be the last lines before the method, with no blank line between the final /// and public static int Add. A blank line detaches the comment from Add. Do not hide the lines inside Main. Do not put them under the return.
Write each documentation line as its own line:
/// <summary>Adds the two stored numbers.</summary>
/// <param name="x">The first number.</param>
/// <param name="y">The second number.</param>
/// <returns>The sum of x and y.</returns>
Then the method:
public static int Add(int x, int y)
return x + y;
The name in each <param> is the parameter's identifier, x or y, in the same spelling. <returns> names the value return hands back. C# uses these XML tags. A Java block that starts with /** and uses @param or @return is a different language's comment, and it is not this page's form. <returns> has an s. @return does not belong in this file.
using System;
public class Pine
{
/// <summary>Adds the two stored numbers.</summary>
/// <param name="x">The first number.</param>
/// <param name="y">The second number.</param>
/// <returns>The sum of x and y.</returns>
public static int Add(int x, int y)
{
return x + y;
}
public static void Main()
{
int x = 10;
int y = 2;
Console.WriteLine("x=" + x);
Console.WriteLine("y=" + y);
int sum = Add(x, y);
Console.WriteLine(sum);
}
}
From the project folder, run dotnet run. The screen shows x=10, then y=2, then 12. Walk Main once. It stores ten and two. The first two prints are the labels you used while debugging. Add(x, y) enters Add with those same numbers. return x + y hands back twelve. The last WriteLine prints that number. None of the /// lines print. If a line of the screen were the summary sentence, the comment would be running as code. It is not.
Read the comment against the signature while the screen stays on 12. <summary> says what Add does, and the body does that with plus. <param name="x"> points at the first parameter. <param name="y"> points at the second. <returns> points at the value the method hands back. Keep the tag names spelled that way, with the angle brackets in place.
A mismatched name, such as name="first" while the parameter is x, can produce a compiler warning. The program can still run, and the last line can still be 12. The warning is about the comment's contract, not about the arithmetic. Fix the name so it matches the parameter. Do not "fix" it by changing the plus.
If the console shows 8, you are still on the minus from the debugging hunt. Switch the body to return x + y and run dotnet run again. If the console shows True and False, you are still on the equals file. Open this Pine file so Main prints the labels and the call to Add.
Twelve still comes from plus
The documentation comment is a label on the method. The number on the screen is the return value. Changing a word inside <summary> does not change 12. Deleting the four /// lines also leaves 12, as long as return x + y stays. That is why you read the comment and the signature together. The screen proves the math. The comment proves you wrote the contract above the method that does the math.
Keep the label prints in Main. They are the debugging page's evidence, and they still show that ten and two went into the call. int sum = Add(x, y) stores what Add returned. You could print Add(x, y) directly. The stored sum makes the last line easy to point at while you compare it with <returns>.
One change at a time still matters. Do not rename x and y, and do not edit the summary, in the same run. If both change and the screen moves, you will not know which edit did it. On this page the screen is supposed to stay x=10, then y=2, then 12 while you only add or adjust the documentation lines.
Paper-check the body the same way as the debugging page. Ten plus two is twelve. The last printed number is 12, so the operator and the screen agree. The new piece is the comment glued to Add, not a new operator.
A double slash is a different note
A line that starts with // is a note beside the code. The comments page already used that form. It does not carry <summary>, <param>, or <returns>, and a documentation tool does not treat it as this method's XML contract. Writing // Adds the two stored numbers. above Add is a useful human note. It is not the documentation comment this page asks for. Keep the three slashes.
The four /// lines are one comment split across lines. Each line repeats ///, then a piece of XML. Together they describe Add. A /// line placed after other statements inside Main documents nothing about Add. The contract belongs immediately above public static int Add.
If the summary text is silly and the body is right, dotnet run can still print x=10, y=2, and 12. The run will not correct your sentence. You correct the sentence by reading it against return x + y. When the sentence says the method adds, and the body adds, the comment matches the code. When the sentence says the method subtracts, fix the sentence. Do not "match" a wrong sentence by putting the minus back.
Angle brackets are part of the tag. <summary> opens and </summary> closes. The same pair shape wraps each <param> and the <returns> element. Leave those brackets in the file. They are markup for the comment, and Main does not print them.
The tags match the signature
Point at <param name="x"> in the comment, then at int x in the signature. Point at <param name="y">, then at int y. Point at <returns>, then at return x + y. That pass is the whole beginner idea. The attribute name is how the first two tags pick a parameter. <returns> has no name attribute, because there is one result, not a named input.
Keep Add public and static so Main can call it without building an object. The call is Add(x, y) with the variables from Main. Those variables are also named x and y. That reuse is allowed. The parameters inside Add are the names the <param> tags talk about. The variables in Main are the values you pass. The labels print the Main variables. The return prints the sum.
Do not start a second public class for the comment. One Pine holds Add and Main. Extra types from earlier modules are history, not this run. Do not wait for the keyboard. Do not call Equals. Do not open a debugger window. The proof on this page is a comment you can read and a screen you already know.
If a tag is missing its closing bracket, the compiler may complain and you may get no x=10 line. That is a broken comment, not a wrong sum. Close the tag, run dotnet run again, and expect the three familiar lines.
Naming stays shut
You can place <summary>, <param name="x">, <param name="y">, and <returns> immediately above Add, keep return x + y, and see x=10, then y=2, then 12. Run Pine with dotnet run. The comment described the method. The plus produced the last number. Choosing better names for x, y, and Add is the next page, naming. This page stops before that job. A rename does not teach the tags, and a tag does not choose a house style for identifiers.
Keep this file as the one you run: documented Add, labeled inputs, last line 12. Keep a stray minus out of the body. Keep naming-rule notes out of this run. When the four /// lines sit tight above Add and the screen is still the debugging page's fixed sum, this lesson did its job.
One last pass through the open file: one Pine, Add above Main, four documentation lines with no blank line under them, return x + y, two labels, no ReadLine, and no Java @param. Run dotnet run once more. If the screen still shows x=10, y=2, and 12, the contract and the sum agree, and the next topic can talk about names.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top