Collections & Methods Lesson 10 of 15
C# Methods — Parameters, Return Values, ref and out
Write reusable C# methods with parameters and return values, use optional and named arguments, overload methods, return several values with tuples, and use ref and out.
A method is a named block of code that does one job. Console.WriteLine and Math.Round are methods. Writing your own methods keeps programs short, organized and easy to fix.
Defining and calling methods
In a top-level program, you can write methods (called local functions) right in the file:
double Average(double a, double b, double c)
{
return (a + b + c) / 3;
}
bool IsPassing(double grade) => grade >= 75;
double avg = Average(88, 91, 85);
Console.WriteLine($"{avg:F2} passed: {IsPassing(avg)}");
Output
88.00 passed: True
- The first word is the return type;
voidmeans nothing is returned. - Method names use PascalCase in C#.
=> expressionis a short form for a method that just returns one expression.
Methods in a class
In the classic structure (and in larger programs), methods live inside a class and are usually static when called from Main:
class Program
{
static void PrintLine(int length)
{
Console.WriteLine(new string('-', length));
}
static void Main()
{
PrintLine(20);
Console.WriteLine("OFFICIAL RECEIPT");
PrintLine(20);
}
}
Output
-------------------- OFFICIAL RECEIPT --------------------
Optional and named arguments
decimal JeepFare(double km, bool discounted = false)
{
decimal fare = 13m + (km > 4 ? (decimal)(km - 4) * 1.80m : 0m);
return discounted ? fare * 0.80m : fare;
}
Console.WriteLine(JeepFare(9));
Console.WriteLine(JeepFare(9, true));
Console.WriteLine(JeepFare(discounted: true, km: 6));
Output
22.00 17.6000 13.2800
Optional parameters (with a default value) must come last. Named arguments let you pass them in any order. Notice that decimal keeps trailing zeros from its calculations; format with :F2 when displaying money.
Overloading
int Area(int side) => side * side;
int AreaRect(int w, int h) => w * h;
Console.WriteLine(Area(5));
Console.WriteLine(AreaRect(4, 6));
Output
25 24
Methods in a class can be overloaded: several methods with the same name but different parameters, like Area(int side) and Area(int w, int h). (Local functions in a top-level program can't share a name, which is why this example uses two names.)
Returning several values with tuples
(int Min, int Max, double Avg) Stats(int[] values)
{
return (values.Min(), values.Max(), values.Average());
}
var (low, high, avg) = Stats(new[] { 88, 72, 95, 81 });
Console.WriteLine($"Lowest {low}, highest {high}, average {avg}");
Output
Lowest 72, highest 95, average 84
ref and out
Normally, a method gets a copy of each argument. ref passes the variable itself so the method can change it; out is for values the method must set:
void AddBonus(ref int score, int bonus)
{
score += bonus;
}
bool TryDivide(int total, int people, out int share)
{
if (people == 0)
{
share = 0;
return false;
}
share = total / people;
return true;
}
int myScore = 80;
AddBonus(ref myScore, 5);
Console.WriteLine(myScore);
if (TryDivide(900, 3, out int each)) Console.WriteLine($"Each pays {each}");
if (!TryDivide(900, 0, out _)) Console.WriteLine("Can't split among zero people");
Output
85 Each pays 300 Can't split among zero people
int.TryParse(text, out int n) follows exactly this pattern. out _ discards a value you don't need.
Recursion
long Factorial(int n) => n <= 1 ? 1 : n * Factorial(n - 1);
Console.WriteLine(Factorial(10));
Output
3628800
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04