C# Basics

C# Methods, Parameters and Tuples

Package reusable work in methods, and return several values at once with a named tuple.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Defining static methods
  • Parameters and expression-bodied methods
  • Returning (int Min, int Max) tuples
Range(new[] { 8, 3, 14, 5 })
  1. 1{ 8, 3, 14, 5 }The argument
  2. 2(values.Min(), values.Max())The body
  3. 3(Min: 3, Max: 14)A named tuple

Input in, results out

Methods group behavior behind a clear contract. Tuples are convenient for returning a small set of related values.

Named tuple elements make the returned values self-documenting without requiring a dedicated class.

Key points

  1. A method has a name, typed parameters and a return type.
  2. => writes a one-line method body.
  3. A tuple such as (int Min, int Max) returns two named values.

Runnable C# example

C#
Run code →
Program.cs
using System;
using System.Linq;

class Program
{
    static (int Min, int Max) Range(int[] values)
        => (values.Min(), values.Max());

    static void Main()
    {
        var range = Range(new[] { 8, 3, 14, 5 });
        Console.WriteLine($"{range.Min}..{range.Max}");
    }
}
Get a Run button for this example Result of running the code
3..14

Practice with the debugger

Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges