Start learning free

LESSON 263 OF 674 · TUPLES, SORTING & BUILT-INS

Tuples in C#: Named Parts and Unpacking

Lesson Read the full explanation

TL ; DR:

A tuple keeps a few values together, like a movie's title and year. Give its parts names and read them with a dot, or unpack them into variables.


(string Title, int Year) movie = ("Frozen", 2013);   // a tuple

Console.WriteLine(movie.Title);                      // Frozen

Console.WriteLine(movie.Year);                       // 2013

var (title, year) = movie;                           // unpacking

Console.WriteLine(title + " " + year);               // Frozen 2013


Full lesson:

Some values belong together. A movie has a title and a year. A point has an x and a y. C# keeps a few values like these in a tuple.

  • ("Frozen", 2013) is a tuple. The values go in parentheses, separated by commas.
  • (string Title, int Year) is its type, with a name for each part.
  • movie.Title is "Frozen", and movie.Year is 2013.
  • Without names, the parts are Item1 and Item2. Names make the code easier to read.

var (title, year) = movie; unpacks the tuple into two new variables, in the same order as its parts.

A method can return a tuple. Its return type is the tuple type, like (string Title, int Year). Then return (a, b); builds it.

Watch the parentheses. (string Title, int Year) is a type, and ("Frozen", 2013) is a value.


Your task

Rex the dog went for a long walk. pet holds his name and his age.

First, make SplitTime return its tuple: the whole hours and the minutes left.

Then unpack pet into name and age.

Last, call SplitTime(135). Unpack its result into hours and mins.

The code already prints the three lines.

Expected output:

Rex
Rex is 4
Rex walked 2 h 15 min
STEP 1 OF 3
    Ctrl + Enter to check

    C# · TUPLES, SORTING & BUILT-INS

    Tuples in C#, explained

    What a tuple is

    Some values belong together: a pet's name and age, a point's x and y. A tuple keeps a few such values in one variable without writing a class for them.

    (string Name, int Age) pet = ("Rex", 4);
    
    Console.WriteLine(pet.Name);   // Rex
    Console.WriteLine(pet.Age);    // 4
    

    Two different things use parentheses here. (string Name, int Age) is a type: it lists each part's type and, optionally, its name. ("Rex", 4) is a value: the parts in order, separated by commas. Read a part with a dot and its name.

    Parts without names

    If you leave the names out, the parts are called Item1, Item2 and so on. Named parts are easier to read, and a named tuple still has Item1 and Item2 underneath:

    var plain = ("Rex", 4);
    Console.WriteLine(plain.Item1 + " " + plain.Item2);
    
    (string Name, int Age) pet = ("Rex", 4);
    Console.WriteLine(pet.Item1 + " " + pet);
    
    Rex 4
    Rex (Rex, 4)
    

    Printing a whole tuple shows its parts in parentheses, separated by a comma and a space.

    Unpacking into variables

    You can take a tuple apart in one line, with the new variables in the same order as the parts:

    var (name, age) = pet;
    Console.WriteLine(name + " " + age);   // Rex 4
    

    The names name and age are yours to choose. Only the order matters. If you only need one part, write a discard, var (_, years) = pet;.

    Returning a tuple from a method

    A method returns one value, but that value can be a tuple. This is the usual reason to reach for one: a method that has two answers.

    public (int Hours, int Mins) SplitTime(int minutes) {
        return (minutes / 60, minutes % 60);
    }
    

    The return type names the parts. return (a, b); builds the tuple. The caller can read SplitTime(135).Hours or unpack the result straight away: var (hours, mins) = SplitTime(135);.

    The lesson's task, step by step

    The starter code has SplitTime with an empty body and a pet tuple. Three things are missing.

    1. Return the tuple. Whole hours are minutes / 60. Integer division throws the remainder away, so 135 / 60 is 2. The minutes left are minutes % 60, which is 15.
    2. Unpack pet. Write var (name, age) = pet; below the first Console.WriteLine. The next line already uses name and age.
    3. Unpack the result of SplitTime(135). A method call that returns a tuple unpacks exactly like a variable does.

    The finished code:

    class Solution {
        public (int Hours, int Mins) SplitTime(int minutes) {
            return (minutes / 60, minutes % 60);
        }
    
        public void MainFunction() {
            (string Name, int Age) pet = ("Rex", 4);
            Console.WriteLine(pet.Name);
    
            var (name, age) = pet;
            Console.WriteLine(name + " is " + age);
    
            var (hours, mins) = SplitTime(135);
            Console.WriteLine(name + " walked " + hours + " h " + mins + " min");
        }
    }
    

    It prints:

    Rex
    Rex is 4
    Rex walked 2 h 15 min
    

    Another use: swapping two variables

    Because a tuple is a value you can build on the spot, it gives C# a short way to swap two variables without a temporary one:

    int x = 3;
    int y = 5;
    (x, y) = (y, x);
    Console.WriteLine(x + " " + y);   // 5 3
    

    The right side builds the tuple (5, 3) first. Then the left side unpacks it into the existing variables. This time there is no var, because x and y already exist.

    Traps

    • Tuples are copied. Assigning a tuple to another variable copies its values. After var a = (1, 2); var b = a; b.Item1 = 9; the variable a is still (1, 2) and b is (9, 2).
    • Order matters, names don't. Unpacking matches parts by position. var (age, name) = pet; compiles, because var accepts any types, but it puts the name in age and the age in name.
    • Mixing up the type and the value. (string Name, int Age) on the right side of = is not a value. Use ("Rex", 4) there.

    Key takeaways

    • A tuple groups a few values: ("Rex", 4), with type (string Name, int Age).
    • Read parts with pet.Name, or unpack them with var (name, age) = pet;.
    • A method can return a tuple when it has more than one answer.
    Keep going with C#500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free