C# Language Tuples Return multiple values from a method

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Tuples can be used to return multiple values from a method without using out parameters. In the following example AddMultiply is used to return two values (sum, product).

void Write()
{
    var result = AddMultiply(25, 28);
    Console.WriteLine(result.Item1);
    Console.WriteLine(result.Item2);
}

Tuple<int, int> AddMultiply(int a, int b)
{
    return new Tuple<int, int>(a + b, a * b);
}

Output:

53
700

Now C# 7.0 offers an alternative way to return multiple values from methods using value tuples More info about ValueTuple struct.



Got any C# Language Question?