C# 演習

Return Statistics with Tuples

Value tuples are useful for small, local groups of related return values.

How it works

Return several named results without creating a class.

Tuple deconstruction assigns each named result to a local variable.

Runnable C# example

C#
Run code →
Program.cs
using System;
using System.Linq;
class Program
{
    static (int Min, int Max, double Average) Stats(int[] values)
        => (values.Min(), values.Max(), values.Average());
    static void Main()
    {
        var (min, max, average) = Stats(new[] { 2, 5, 8 });
        Console.WriteLine($"{min}, {max}, {average:F1}");
    }
}
Expected output
2, 8, 5.0

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.