Tuple is a new generic type in C# and also in F#. It can be used as an immutable data container. It can hold up to 8 elements of same or different types. If more than 8 elements are required then 8th element can be defined as another tuple.
Like anonymous type, tuple don’t need to define new behavior for collection. It can be specified as parameter or return type to a method.
There are two ways to instantiate a tuple:
var tuple1 = new TupleTo compare between two tuples using Equal method.
var tuple1 = new TupleAbove code will print “Tuples are equal”.
When using “==” operator, two tuples are considered equal if they reference same tuple object.
var tuple1 = new TupleAbove code will print “Tuples are Not equal”.
The members of tuples are called “Items”. The items are named based on their position in the tuple. E.g. first item is named as Item1, second item is named as Item2 and so on.
var tuple1 = new TupleTuple does not implement IEnumerable interface, hence its elements can not be enumerated. It can also not be used in foreach loop. Since Tuple is not sealed, if you want to use foreach on Tuple then you can inherit Tuple class and implement the child class with IEnumerable interface and provide definition for GetEnumerator method.
var tuple1 = new TupleAbove code will throw compile time error.
Hope this helps,