4.0 What This Teaches
- Basic method syntax: return type, name, parameters, body
- Parameter passing: by value,
ref, andout - Default parameter values and method overloading
- Returning multiple values with tuples
- Expression-bodied methods and local functions
4.1 Method Syntax
static int Add(int a, int b)
{
return a + b;
}
static void Greet(string name)
{
Console.WriteLine($"Hello, {name}!");
}
// calling them
int result = Add(3, 4);
Greet("Alice");
void means the method returns nothing.
static is required in top-level statement context.
4.2 Parameter Passing
ref to pass by reference, or out to return a
value through a parameter:
static void Double(ref int x)
{
x *= 2; // modifies caller's variable
}
static bool TryDivide(int a, int b, out double result)
{
if (b == 0) { result = 0; return false; }
result = (double)a / b;
return true;
}
int n = 5;
Double(ref n);
Console.WriteLine(n); // 10
if (TryDivide(10, 3, out double quotient))
Console.WriteLine($"quotient = {quotient:F3}");
out parameters must be assigned before the method returns. The caller
does not need to initialize them before passing.
4.3 Default Parameters and Named Arguments
static void Log(string message, string level = "INFO", bool timestamp = false)
{
string prefix = timestamp ? $"[{DateTime.Now:HH:mm}] " : "";
Console.WriteLine($"{prefix}[{level}] {message}");
}
Log("Server started"); // uses defaults
Log("Disk full", "WARN"); // overrides level
Log("Crash", level: "ERROR", timestamp: true); // named arguments
4.4 Method Overloading
static void Print(int x) => Console.WriteLine($"int: {x}");
static void Print(double x) => Console.WriteLine($"double: {x}");
static void Print(string s) => Console.WriteLine($"string: {s}");
Print(42); // int: 42
Print(3.14); // double: 3.14
Print("hello"); // string: hello
4.5 Returning Multiple Values
static (int min, int max) MinMax(int a, int b)
{
return a < b ? (a, b) : (b, a);
}
var (lo, hi) = MinMax(9, 3);
Console.WriteLine($"min={lo}, max={hi}"); // min=3, max=9
min, max) make the return type
self-documenting. Callers can also access elements by position: .Item1,
.Item2.
4.6 Expression-Bodied Methods
=> shorthand:
static int Square(int x) => x * x;
static bool IsEven(int n) => n % 2 == 0;
static double CircleArea(double r) => Math.PI * r * r;
4.7 Local Functions
static long Factorial(int n)
{
if (n < 0) throw new ArgumentException("n must be non-negative");
return Compute(n);
long Compute(int k) => k <= 1 ? 1 : k * Compute(k - 1);
}
Console.WriteLine(Factorial(10)); // 3628800
4.8 Example - All Together
// Functions - method definitions and calling patterns.
Console.WriteLine(Add(3, 4));
Console.WriteLine(Describe(42));
Console.WriteLine(Describe("hello"));
var (lo, hi) = Bounds(-5, 12, 3, -1, 7);
Console.WriteLine($"lo={lo}, hi={hi}");
static int Add(int a, int b) => a + b;
static string Describe(int x) => $"int: {x}";
static string Describe(string s) => $"string: {s}";
static (int lo, int hi) Bounds(params int[] values)
{
int lo = values[0], hi = values[0];
foreach (int v in values)
{
if (v < lo) lo = v;
if (v > hi) hi = v;
}
return (lo, hi);
}
7
int: 42
string: hello
lo=-5, hi=12
4.9 Exercise
Exercise
- Write a
Clamp(int value, int min, int max)method that returnsvalueclamped to the range [min, max]. Write it as an expression-bodied method. - Write overloads of
Clampfordouble. - Write a
Swap<T>(ref T a, ref T b)method that swaps two values. Test it with bothintandstring.
4.10 Common Mistakes
Forgetting return in a non-void method
return statement,
the compiler reports "not all code paths return a value". Every path through the
method must return a value of the declared type.
Expecting ref to work with value type copies
static void Double(int x) { x *= 2; } // modifies a copy, not the caller's variable
int n = 5;
Double(n);
Console.WriteLine(n); // still 5, not 10
ref to both the method signature and the call site to modify the caller's variable.Ambiguous overloads
int and another
takes long and you pass a literal 0), the compiler reports
an ambiguous call error. Make the call explicit with a cast: Method((long)0).
4.11 Key Terms
| Term | Meaning |
|---|---|
| method | A named block of code with a return type and parameters |
| void | Return type indicating the method returns no value |
| ref | Passes a variable by reference; caller and callee share the same location |
| out | Like ref but the parameter does not need to be initialized before calling |
| overloading | Multiple methods with the same name but different parameter lists |
| default parameter | A parameter with a fallback value used when the caller omits it |
| named argument | Specifying a parameter by name rather than position at the call site |
| tuple | A lightweight grouping of values with optional named fields |
| expression-bodied member | Method written as => expression instead of a block |
| local function | A function defined inside another method; not visible outside it |