Tuple?
A tuple is a data structure that has a specific number and sequence of elements.
A tuple allows you to combine multiple values of possibly different types into a single object without having to create a custom class. This can be useful if you want to write a method that for example returns three related values but you don’t want to create a new class.
System.Tuples
namespace supports Tuple class in C#.
The purpose of a tuple is to create a way to return multiple values from a function.
class TupleExample { static void Main() { // Create three-item tuple. Tuple<int, string, bool> tuple = new Tuple<int, string, bool>(10, "csharpstar", true); // Access tuple properties. if (tuple.Item1 == 10) { Console.WriteLine(tuple.Item1); } if (tuple.Item2 == "easywcf") { Console.WriteLine(tuple.Item2); } if (tuple.Item3) { Console.WriteLine(tuple.Item3); } } }
Ç?kt?:
10
True
Tuple.Create method :
class TupleExample { static void Main() { // Use Tuple.Create static method. var tuple = Tuple.Create("Csharpstar", 10, true); // Test value of string. string value = tuple.Item1; if (value == "csharpstar") { Console.WriteLine(true); } // Test Item2 and Item3. Console.WriteLine(tuple.Item2 == 10); Console.WriteLine(!tuple.Item3); // Write string representation. Console.WriteLine(tuple); } }
Ç?kt?:
true
true
false
<Csharpstar”, 10, true>
Tuple that returns multiple values in C#:
class TupleExample { static Tuple<string, int> NameAndId() { // This method returns multiple values. return new Tuple<string, int>("Csharpstar", 10); } static void Main(string[] args) { var result = NameAndId(); string name = result.Item1; int id = result.Item2; // Display the multiple values returned. Console.WriteLine(name); Console.WriteLine(id); } }
Ç?kt?:
10
When to use Tuples in C#?
Tuples are commonly used in four ways:
- To represent a single set of data. For example, a tuple can represent a database record, and its components can represent individual fields of the record.
- To provide easy access to, and manipulation of, a data set.
- To return multiple values from a method without using out parameters
- To pass multiple values to a method through a single parameter. For example, the Thread.Start(Object) method has a single parameter that lets you supply one value to the method that the thread executes at startup time. If you supply a Tuple object as the method argument, you can supply the thread’s startup routine with three items of data.
Issues on Tuple Class:
Member Names:
The main problem with Tuple class is it’s Member Names. It does not have any Semantic meaning.
If you see a return type of Tuple that doesn’t really tell you anything.
So it is difficult to understand and maintain the code. It would be far more useful if the return type were something like Tuple
So in C#7, Tuples would probably have names, as it makes identifying each member of the Tuple easier.
Let’s look at below example to understand the C#7 proposal on Tuples.
public( int sum,int count) Tally(IEnumerable <int> values) { var s=0; var c=0; foreach (var value in values) { s+= values; c++; } return (s,c); }
There are 2 proposals being considered for how tuples would be returned from function:
//1st proposal var t = Tally(myValues); Console.WriteLine($"Sum: {t.sum}, Count: {t.count}"); //2nd proposal (var s,var c) = Tally(myValues); Console.WriteLine($"Sum: {s}, Count: {c}");
Tuples and Async :
public async Task<( int sum,int count)> TallyAsync(IAsyncEnumerable <int> values) { var s=0; var c=0; foreach (await var value in values) { s+= values; c++; } return (s,c); } var t = await Tally(myValues); Console.WriteLine($"Sum: {t.sum}, Count: {t.count}"); (var s,var c) = await Tally(myValues); Console.WriteLine($"Sum: {s}, Count: {c}");