Lesson 11 of 30
Methods and Parameters
C# in Visual Studio 2026 — a hands-on guide for developers at every level.
Defining a Method
static int Add(int a, int b)
{
return a + b;
}
Console.WriteLine(Add(3, 7)); // 10
Expression-Bodied Methods
For single-expression methods, you can use the arrow syntax:
static int Square(int n) => n * n;
static string Greet(string name) => $"Hello, {name}!";
Default and Named Parameters
static void PrintInfo(string name, int age = 0, string city = "Unknown")
=> Console.WriteLine($"{name}, {age}, {city}");
PrintInfo("Alice"); // Alice, 0, Unknown
PrintInfo("Bob", 25); // Bob, 25, Unknown
PrintInfo("Carol", city: "Paris"); // Carol, 0, Paris
ref and out Parameters
static void Swap(ref int x, ref int y)
{
int temp = x; x = y; y = temp;
}
int a = 5, b = 10;
Swap(ref a, ref b);
Console.WriteLine($"{a}, {b}"); // 10, 5
params Array
static int Sum(params int[] numbers)
=> numbers.Sum(); // using LINQ
Console.WriteLine(Sum(1, 2, 3)); // 6
Console.WriteLine(Sum(10, 20, 30, 40)); // 100