C# 7.0 New Features – Tuples Enhancements
Many a times we would like to return more than one values from a method call. C# provided lot of features to support this functionality. Some of them are:
- Out Parameter
- Ref Parameter
- Anonymous types can be returned via dynamic keywords
- Tuples
C# 4.0 introduced a type Tuple<…> using which you can return more than one value from a function without creating a new type. Tuple behaves like a dictionary with each returned item accessible as Item1, Item2 and so on. This is very verbose and require a Tuple object to be created.
Example using C# 4.0
Tuple<int, int> ReturnRuntimeTimeEncapsulatingTwoValues() { return new Tuple<int, int>(1, 2); } private void btnTuple6_Click(object sender, EventArgs e) { Tuple<int, int> tuple = ReturnRuntimeTimeEncapsulatingTwoValues(); //Usage in c# 6 int value1 = tuple.Item1; //No control over member name and its too unintuitive int value2 = tuple.Item2; }