Methods, Parameters, and Tuples
Define reusable methods with named results and multiple return values.
Introduction
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.
Runnable C# example
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}");
}
}
3..14Practice 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.
- Distance Between Points Compute the straight-line distance between two points. Easy
- Days in a Month Return how many days a month has, including leap years. Easy