神刀安全网

C# 7 Potential Features – Tuples

In this article, we will discuss on Tuple Class in C# and the potential new proposals that is being considered for C#7.

What is Tuple in C#?

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.

Example:

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);             }         }     }
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);             }         }     } 

Output:

10 True
10 True 

Create Tuple using 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);         }     }
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);         }     } 

Output:

True True False <Csharpstar,10, True>
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);         }     }
 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);         }     } 

Output:

Csharpstar 10
Csharpstar 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

C# 7 Potential Features – Tuples

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 &lt;int&gt; values) {    var s=0; var c=0;    foreach (var value in values) { s+= values; c++; }    return (s,c); }
public( int sum,int count) Tally(IEnumerable <int> values) {   var s=0; var c=0;   foreach (var valuein 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}");
//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 :

You can use Tuples with Async.

public async Task&lt;( int sum,int count)&gt; TallyAsync(IAsyncEnumerable &lt;int&gt; 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}");
public asyncTask<( int sum,int count)> TallyAsync(IAsyncEnumerable <int> values) {   var s=0; var c=0;   foreach (awaitvar valuein values) { s+= values; c++; }   return (s,c); }   var t = awaitTally(myValues); Console.WriteLine($"Sum: {t.sum}, Count: {t.count}");   (var s,var c) = awaitTally(myValues); Console.WriteLine($"Sum: {s}, Count: {c}"); 

In the next article, we will discuss on C#7, potential feature: Pattern matching.

Thanks for visiting !!

© 2016,admin. All rights reserved.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » C# 7 Potential Features – Tuples

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮