← Back to C# Course | Chapter 15: Modern C# Features | Lesson 3 of 6

Tuples

In this page:

  1. Tuples

Tuples

ValueTuple lets a method return multiple values without declaring a dedicated class, written as (int, string) or with named elements. This is lighter weight than a full class when you just need to group a few related values temporarily. Tuples are value types, so they're compared and copied by value.

Note: Named tuple elements, like (int Count, string Label), make the returned values much more readable at the call site.

Example: Tuples

markup
using System;

class Program
{
    static (int Count, double Average) Analyze(int[] numbers)
    {
        int sum = 0;
        foreach (int n in numbers) sum += n;
        return (numbers.Length, (double)sum / numbers.Length);
    }

    static void Main(string[] args)
    {
        var result = Analyze(new int[] { 4, 8, 15 });
        Console.WriteLine(result.Count);
        Console.WriteLine(result.Average);
    }
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.