Methods
A method is a named block of code you write once and call by name as often as you need. Read the signature int Add(int a, int b), then follow two calls step by step: the arguments are copied into the parameters of a new frame on the call stack, the body runs, and return sends one value back to the caller before the frame goes away. The program prints 15.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01In the signature static int Add(int a, int b), what does the first int tell you?
The type before the name is the return type. int Add(...) promises that every call gives back an int, which is why int x = Add(2, 3); compiles.
02Line 2 runs int y = Add(x, 10); while x holds 5. What is in the parameter a while Add runs?
Arguments are passed by value by default: the value of x, 5, is copied into a. If Add changed a, x would still be 5.
03What happens to Add's frame on the call stack when return a + b; runs?
return ends the call and hands one value back. The frame that held a and b is no longer needed, so it is removed; a and b only exist during the call.
04Console.WriteLine has the return type void. What does that mean?
void means the method returns no value. It does its job (printing) and the call ends; int z = Console.WriteLine(y); would not compile.
05Why does Add("2", 3) never run?
C# checks every call when it builds. A string cannot be converted to the int parameter a, so you get error CS1503 and nothing runs.
Glossary
- Method
- A named block of code that does one job. You define it once and run it by calling its name.
- Signature
- The first line of a method: its return type, its name and its parameter list, such as int Add(int a, int b).
- Parameter
- A named, typed input in the signature (a and b). It works like a local variable that exists only during a call.
- Argument
- The value you pass in a call, such as 2 and 3 in Add(2, 3). By default it is copied into the parameter.
- Return value
- The one value a method hands back to its caller with return. Its type is the method's return type.
- void
- A return type meaning the method hands nothing back, as with Console.WriteLine.
- Call stack
- Memory for running methods. Each call gets a frame on top, holding its parameters and locals; it is removed when the call returns.
Notes and sources
- With top-level statements, a method written in Program.cs is strictly a local function; the compiler turns it into an ordinary method. static means it cannot use the top-level variables x and y, only its own parameters. In larger programs methods live inside a class.
- The call stack here is simplified: a real frame also holds the return address and other bookkeeping, and the JIT compiler may keep small values like a and b in CPU registers or inline a tiny method like Add.
- Arguments are passed by value by default: the parameter gets a copy. For an int that copy is the number itself. The keywords ref, out and in pass a variable by reference instead.
- The program is real C#. Put the eight lines in Program.cs of a new console project and dotnet run prints 15.
- Microsoft Learn: Methods in C#
- Microsoft Learn: Methods (C# programming guide)
- Microsoft Learn: Method parameters (passed by value by default)
- Microsoft Learn: Jump statements (the return statement)
- Microsoft Learn: void
- Microsoft Learn: Local functions
- Microsoft Learn: Top-level statements
- Microsoft Learn: Compiler error CS1503
- Microsoft Learn: Errors for parameter and argument mismatches (CS7036)
- ECMA-334: C# Language Specification
Quiz Unlocks after the last step.