C# Method Parameters
Parameters are the variables listed in a method's declaration; arguments are the actual values you pass when calling it. Parameters let one method work with different data each time it is called.
Passing arguments
Each parameter has a type and a name. Arguments are matched to parameters by position.
using System;
class Program
{
static void Greet(string name, int age)
{
Console.WriteLine(quot;{name} is {age} years old.");
}
static void Main()
{
Greet("Liam", 31);
Greet("Noor", 8);
}
}Liam is 31 years old.
Noor is 8 years old.
The number and types of arguments must match: Greet("Liam") or Greet(31, "Liam") do not compile.
Passing by value
By default C# passes arguments by value: the method receives a copy. Changing a value-type parameter inside the method has no effect on the caller's variable.
using System;
class Program
{
static void Double(int number)
{
number *= 2;
Console.WriteLine(quot;Inside: {number}");
}
static void Main()
{
int x = 10;
Double(x);
Console.WriteLine(quot;Outside: {x}");
}
}Inside: 20
Outside: 10
For reference types such as arrays and objects, the copied value is the reference. The method cannot make the caller's variable point somewhere else, but it can modify the object both share:
using System;
class Program
{
static void Fill(int[] data)
{
for (int i = 0; i < data.Length; i++)
data[i] = i * i;
}
static void Main()
{
int[] numbers = new int[5];
Fill(numbers);
Console.WriteLine(string.Join(" ", numbers));
}
}0 1 4 9 16
ref, out and in
Modifiers change how an argument is passed. The modifier is written on both the parameter and the argument (except in, which is optional at the call site).
| Modifier | Meaning | Must be initialised before the call? | Must be assigned in the method? |
|---|---|---|---|
ref |
Pass the variable itself; the method may read and change it | Yes | No |
out |
The method outputs a value through this parameter | No | Yes |
in |
Pass by reference, read-only (avoids copying large structs) | Yes | Not allowed |
using System;
class Program
{
static void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
static void MinMax(int[] values, out int min, out int max)
{
min = values[0];
max = values[0];
foreach (int v in values)
{
if (v < min) min = v;
if (v > max) max = v;
}
}
static void Main()
{
int x = 1, y = 2;
Swap(ref x, ref y);
Console.WriteLine(quot;x={x}, y={y}");
MinMax(new[] { 8, 3, 15, 6 }, out int lo, out int hi);
Console.WriteLine(quot;min={lo}, max={hi}");
if (int.TryParse("123", out int parsed))
Console.WriteLine(parsed + 1);
}
}x=2, y=1
min=3, max=15
124
int.TryParse is the most famous out user in the standard library: it returns a bool and outputs the parsed number. You can declare the out variable inline (out int parsed), or discard it with out _ if you do not need it.
Tip: When a method needs to return several values, a tuple return value (next lesson) is usually cleaner than multiple
outparameters.
params: a variable number of arguments
A params array parameter accepts any number of arguments — or an array. It must be the last parameter.
using System;
class Program
{
static int Sum(params int[] numbers)
{
int total = 0;
foreach (int n in numbers) total += n;
return total;
}
static void Log(string level, params string[] parts)
{
Console.WriteLine(quot;[{level}] {string.Join(" | ", parts)}");
}
static void Main()
{
Console.WriteLine(Sum());
Console.WriteLine(Sum(5));
Console.WriteLine(Sum(1, 2, 3, 4));
Console.WriteLine(Sum(new[] { 10, 20 }));
Log("INFO", "server started", "port 8080");
}
}0
5
10
30
[INFO] server started | port 8080
Console.WriteLine("{0} {1}", a, b) and string.Join use params too.
Test yourself
static void Inc(int n) { n++; } — after int a = 5; Inc(a);, what is a?
- 5
- 6
- 0
- It does not compile
Which modifier requires the method to assign the parameter before returning?
- ref
- in
- out
- params
Where must a params parameter appear?
- First
- Last
- Anywhere
- Only in static methods