C# Methods

Parameters, overloading, out and named arguments.

✍️ 2 exercises in this lesson
  • static int Add(int a, int b) => a + b;: an expression-bodied method.
  • Optional parameters: void Greet(string name = "Guest")
  • Named arguments: Greet(name: "Ali")
  • Overloading: same name, different parameters.
  • out and ref parameters return extra values.

Key Takeaways: C# Methods

Mastering C# Methods helps you write clean and reliable C# code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

C# Methods Code Example

C#
static int Add(int a, int b) => a + b;
static double Add(double a, double b) => a + b;
static string Greet(string name = "Guest", string greeting = "Hello") => $"{greeting}, {name}!";

static void MinMax(int[] values, out int min, out int max)
{
    min = values.Min();
    max = values.Max();
}

Console.WriteLine(Add(2, 3));
Console.WriteLine(Add(2.5, 3.5));
Console.WriteLine(Greet());
Console.WriteLine(Greet(greeting: "Salam", name: "Usman"));

MinMax(new[] { 8, 3, 11 }, out int lo, out int hi);
Console.WriteLine($"{lo}..{hi}");
Output
5
6
Hello, Guest!
Salam, Usman!
3..11
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

C# Methods Practice Exercises

Check what you learned in “C# Methods”.

/ 2 solved
Fill in the blank

Return an extra value through a parameter.

static void MinMax(int[] v,  int min, out int max)
✓ Correct! Not quite. Try again.
Multiple choice

What does an expression-bodied method look like?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services