Problem overview
This lesson is less about the sum itself and more about how challenges work. Every challenge gives you some input and asks for code that turns it into the right output. Here the input is two numbers, a and b, and the output is their sum. For a = 4 and b = 9 the answer is 13, because 4 + 9 = 13.
How a challenge is laid out
The editor always gives you the same skeleton:
- The class. Everything sits inside
class Solution { ... }. In C#, code lives inside classes. You don't need to know more than that yet. Leave the class as it is. - The method. Inside the class there is one method, here
public int Sum(int a, int b). A method is a function that belongs to a class. This is where your solution goes. - The parameters.
int a, int bare the inputs from the statement. Their names match the names in the statement, and each has the typeint, a whole number. - The return type. The
intbefore the method name says the method gives back a whole number. Your job is toreturnthe value the statement calls the output.
The word public says the method can be called from outside the class, which the lesson's tests need to do. The method name starts with a capital letter, Sum, because C# methods conventionally use PascalCase.
Approach
There is nothing to search for or loop over. You need the sum of the two inputs, and C# has the + operator for that. The only thing to remember is that a challenge method must return its answer, not print it. The tests call Sum(4, 9) and looks at the value that comes back.
Step by step
- Find the comment
// Write the first step below:inside the method body. - Add the two parameters with
a + b. - Put
returnin front of that expression, so the result goes back to whoever calledSum, and end the line with a semicolon.
The solution in C#
class Solution {
public int Sum(int a, int b) {
// Write the first step below:
return a + b;
}
}
Some C# details in these few lines:
- The statement ends with
;. Forgetting it is the most common first compile error. - If the method has a non-
voidreturn type likeint, every path must end with areturnof that type. An empty body fails to compile for this reason, which is why the starter code doesn't compile until you add your line. - Adding two
intvalues gives anint, so the result matches the declared return type with no conversion.
Edge cases and common mistakes
- Printing instead of returning.
Console.WriteLine(a + b);shows 13 on screen but returns nothing, so the method won't compile with theintreturn type and the tests get no value. - Overflow. An
intholds values up to 2,147,483,647. By default C# does not report overflow.Sum(2147483647, 1)silently wraps around to-2147483648. The lesson's tests use small numbers, but it's worth knowing thatintarithmetic has limits. - Negative numbers. They work with no special handling:
Sum(-4, 9)returns 5. - Changing the signature. Renaming the method, or changing its parameter types or return type, stops the tests from calling it.
Testing
Try a few inputs in the editor above. Sum(4, 9) should return 13, Sum(0, 0) should return 0, and Sum(-4, 9) should return 5.