Tuesday, September 13, 2016

C# 7.0 Potential Features – Tuples

In this article, let's discuss on Tuple Class in C# and the potential new proposals that are being considered for C#7.

Tuple Class C# 7
Potential Features - Tuple



What is Tuple in C#?

Basically, a tuple (Tuple in C#) is an ordered sequence, immutable, fixed-size and of heterogeneous objects, i.e., each object being of a specific type. The tuples are not new in programming. They are already used in F#, Python, and databases.

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 CodeYourWayTupleDemo
{
static void Main()
{
// Create three-item tuple.
Tuple tuple = new Tuple(10, "CodeYourWay", true);
// Properties Accessor
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



Tuple that returns multiple values in C#:

      

class CodeYourWayTupleDemo
{
static Tuple PageNameAndLikes()
{
// This method returns multiple values.
return new Tuple("fb.com/CodeYourWay", 250);
}

static void Main(string[] args)
{
var result = PageNameAndLikes();
string pageName = result.Item1;
int likes = result.Item2;
// Display the multiple values returned.
Console.WriteLine("Visit Link-" + pageName);
Console.WriteLine(likes);
}
}

Output:

Visit Link-fb.com/CodeYourWay
250


Now let's learn 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.

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) CodeYourWayDemo(IEnumerable <int> values)
{
var s=0; var c=0;
foreach (var value in values) { s+= values; c++; }
return (s,c);
}


There are 2 proposals/ways being considered for how tuples would be returned from function:
//1st way
var t = CodeYourWayDemo(demoValues);
Console.WriteLine($"Sum: {t.sum}, Count: {t.count}");

//2nd way
(var s,var c) = CodeYourWayDemo(demoValues);
Console.WriteLine($"Sum: {s}, Count: {c}");


Tuples and Async :

We can even use Tuples with Async !!!!!.

public async Task<( int sum,int count)> CodeYourWayAsync(IAsyncEnumerable <int> values)
{
var s=0; var c=0;
foreach (await var value in values) { s+= values; c++; }
return (s,c);
}

var t = await
CodeYourWayAsync(demoValues);
Console.WriteLine($"Sum: {t.sum}, Count: {t.count}");

(var s,var c) = await
CodeYourWayAsync(demoValues);
Console.WriteLine($"Sum: {s}, Count: {c}");

In the next article, we will discuss one more C#7, potential feature: Local Func's
Thanks for visiting !!
© 2016CodeYourWay. All rights reserved.