C# Naming
Give the sum names a reader can say again, without changing the numbers. Inside Pine, a muddy method can still add and Main can still print x=10, then y=2, then 12. A second file names that method Add, names the values left and right, and prints left=10, then right=2, then 12. Leave formatting for the next page. This lesson is spelling. It is not a new way to print.
Quality and Wrap-up continues, so the file stays Pine and the numbers stay 10 and 2. The XML docs page put four /// lines above Add, and the screen showed x=10, then y=2, then 12. The attributes <param name="x"> and <param name="y"> are tied to the spellings x and y. When you rename the parameters, change those name attributes in the same edit. If you leave the old spellings, the tags point at identifiers the method no longer has. A new name does not turn 12 into another number. The plus does that work. If the body still says minus, the last line is still 8, however tidy the name looks.
int, return, and Console.WriteLine are already in the course. A name is the spelling you repeat when you want that same slot again. C# treats a different capital letter as a different name.
A short name still adds
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 the sum, leftover Equals calls, and any ReadLine line. The program must not wait for typing.
For the first demo, keep the four documentation lines, and give the method a name that does not say what it does. The parameters become a and b. The tags still say x and y. That mismatch is the mistake you are about to see, not the style you will keep.
public static int Go(int a, int b)
return a + b;
Main can keep the old locals and the old labels, and call the muddy method:
int x = 10;
int y = 2;
int sum = Go(x, y);
The letters a and b are legal names. They do not tell a reader which one is the first stored number. Go does not say the method adds. The screen can still be right while the names are poor.
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 Go(int a, int b)
{
return a + b;
}
public static void Main()
{
int x = 10;
int y = 2;
Console.WriteLine("x=" + x);
Console.WriteLine("y=" + y);
int sum = Go(x, y);
Console.WriteLine(sum);
}
}
The screen ignores the muddy name
Run dotnet run. The screen shows x=10, then y=2, then 12. Walk Main once. It stores ten and two under x and y. Go(x, y) passes those values into a and b. return a + b hands back twelve. The last print is that number. Nothing in the name Go was executed as a word. The plus ran.
The documentation lines did not run either. They still mention x and y, while the parameters are a and b. A reader who trusts the tags looks for parameters that are not there. A compiler warning about those names can appear, and the last line can still be 12. The warning is about the contract. The 12 is about the plus.
If the last line is 8, the body is still the minus from the debugging page. Rename nothing until the operator is plus again. A prettier name will not fix subtraction. If the screen shows True and False, you are still on the equals file. Open this Pine file.
Say the split while you look: the screen tells you the math, and the spellings tell you whether a person can find the math later. Both have to be right before you leave the file. This first demo keeps a bad spelling on purpose so you can see that 12 survives it.
Spell the role
The second file keeps the same numbers and the same plus. The method name becomes Add, which starts with a capital letter. The parameters and the locals become left and right, which start with a small letter. The labels change with the locals, so the screen says what the variables are called. The name attributes change to left and right as well.
public static int Add(int left, int right)
return left + right;
int left = 10;
int right = 2;
Types and methods in this course use PascalCase: Pine, Add, Main. A capital letter starts the name, and each later word inside the name would start with a capital too. Parameters and locals use camelCase: left, right, and the earlier sum. A small letter starts the name. Add is the method. left is a value. The two shapes sit next to each other so you can tell a method from a variable at a glance.
using System;
public class Pine
{
/// <summary>Adds the two stored numbers.</summary>
/// <param name="left">The first number.</param>
/// <param name="right">The second number.</param>
/// <returns>The sum of left and right.</returns>
public static int Add(int left, int right)
{
return left + right;
}
public static void Main()
{
int left = 10;
int right = 2;
Console.WriteLine("left=" + left);
Console.WriteLine("right=" + right);
int sum = Add(left, right);
Console.WriteLine(sum);
}
}
Run dotnet run again. The screen shows left=10, then right=2, then 12. The numbers did not change. The spellings did. left holds ten, right holds two, and Add returns their plus. The labels match the variables, so a reader does not have to guess that x meant the left input.
Read the signature once: Add(int left, int right). Then read the call: Add(left, right). The same spellings appear in both places. That repetition is the point of a name. If the call says Add(right, left) you have swapped the arguments. With these two numbers the plus hides the swap, because ten plus two equals two plus ten. Choose names you would still trust when the operation stops being symmetric.
Keep one public class Pine. Do not wait for the keyboard. Do not call Equals. The proof is two runs of the same math under two naming choices, and only the second choice is the one you keep.
The tag uses the same spelling
<param name="left"> must spell left the way the parameter spells it. <param name="right"> must spell right. The attribute is not a nickname and not a translation. It is the identifier. On the XML docs page those attributes said x and y because the parameters were x and y. After this rename, x and y are gone from Add. Leaving name="x" would document a parameter the method does not have.
<summary> and <returns> should use the same words as the code when they mention the values. <returns>The sum of left and right.</returns> matches return left + right. A summary that says the method subtracts is a naming lie even when the screen shows 12. Fix the sentence. Do not put the minus back to match a bad sentence.
Change the tags in the same edit as the parameters. One run, one naming pass. If you rename left to something else and forget the attribute, the next dotnet run can still print left=10 only if the local is still called left. The screen does not check the comment. You do.
Case makes a second name
Add and add are two names. The method you wrote is Add. A call spelled add(left, right) does not find it. The build stops, and you do not get left=10. Fix the call to Add. Do not add a second method just to satisfy a lowercase spelling.
left and Left are two names as well. A local int Left = 3 would be a different slot from int left = 10. Printing one of them does not print the other. That is why the casing rule is practical here, not decoration. Methods and types start with a capital. Locals and parameters start with a small letter. Following that split keeps Add and left from looking like the same kind of thing.
A name cannot start with a digit. int 2right = 2 does not build. Start with a letter: right. A keyword cannot be a name either. int class = 10 does not build, because class is already the word that starts a class. Pick kind or another free word if you need that idea. Pine itself stays the class name for this course. You are not renaming the file's class on this page.
Spaces inside a name do not build. int left value is two words where one name belongs. Use leftValue if you later need two words in a local: a small letter, then a capital on the second word. That is camelCase. LeftValue would be PascalCase, the shape you are using for methods and types, not for this local.
Formatting stays shut
You can run the muddy Go file and see x=10, then y=2, then 12. You can run the Add file and see left=10, then right=2, then 12. The plus is the same. The spellings got specific, the method name says add, and the name attributes match left and right. How those numbers are spaced, padded, or written with extra text around them is the next page, formatting. This page stops before that job. A format pattern does not choose a better identifier, and a better identifier does not change 12.
Keep the second file as the one you run. Keep return left + right. Keep the four /// lines tight above Add, with name="left" and name="right". Keep a minus out of the body. When dotnet run shows left=10, right=2, and 12, and the tags use those same spellings, this lesson did its job.
One last pass: one Pine, method Add in PascalCase, locals left and right in camelCase, no ReadLine, no call spelled add, and no name that starts with a digit. Run dotnet run once more. If the screen still shows those three lines, the names and the sum agree, and the next topic can change how a value is written.
🧠 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