Skip to content

C# Parameters 👷🏼‍♂️👷🏼‍♀️

Pass by Ref & by Out

void Foo(ref int x) { ... }  // method signature with ref
Foo(ref x);  // calling method

void Foo(out string name) { ... }  // method signature with out
Foo(out x);  // calling method

out parameters do not need to be defined before being passed. They must be assigned in the function before returning. ref parameters need to be defined before being passed in.

Optional Parameters

void Foo(int x = 42) { ... };

Named parameters

void Foo(int x = 10, int y = 20 ) { ... }
Foo(x:1, y:2);  <-- x= 1,  y= 2
Foo(y:3);       <-- x=10,  y= 3
Foo(y:3, x:4);  <-- x= 4,  y= 3
Foo(y:9);       <-- x=10,  y= 9
Foo(100, y:20); <-- x=100, y= 20  // positional BEFORE named parameter