C# Language Tuples Creating tuples

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 are created using generic types Tuple<T1>-Tuple<T1,T2,T3,T4,T5,T6,T7,T8>. Each of the types represents a tuple containing 1 to 8 elements. Elements can be of different types.

// tuple with 4 elements
var tuple = new Tuple<string, int, bool, MyClass>("foo", 123, true, new MyClass());

Tuples can also be created using static Tuple.Create methods. In this case, the types of the elements are inferred by the C# Compiler.

// tuple with 4 elements
var tuple = Tuple.Create("foo", 123, true, new MyClass());
7.0

Since C# 7.0, Tuples can be easily created using ValueTuple.

var tuple = ("foo", 123, true, new MyClass());

Elements can be named for easier decomposition.

(int number, bool flag, MyClass instance) tuple = (123, true, new MyClass());


Got any C# Language Question?