a blog for those who code

Friday 10 February 2017

You Will Never Thought That Knowing C#7 Tuples Could Be So Beneficial

In this post we will be discussing about C# 7 Tuples using Visual Studio 2017 RC. Microsoft has announced a lot of new useful features that make C# development a lot easier in C# 7, and one of the feature is Tuples. You will never have thought that knowing it could be so beneficial in developing applications in C#.

In simple terms a tuple is a finite ordered list of elements. In C# 7 a new feature is introduced to improve the support of tuples. This is done to improve the performance and software design. It helps in improving the performance because you don't need to store a tuple as an instantiated class to use it in your code anymore thus it removes the memory overhead.

Prerequisites for using Tuples


You need to have latest update of Visual Studio 2015 or the Visual Studio 2017 RC to take advantage of the tuple value type. We will be using Visual Studio 2017. You also need to install ValueTuple NuGet Package to work with tuple value types in your project.

PC : https://www.nuget.org/packages/System.ValueTuple

You might be wondering that C# has Tuple Class then how it is different that the new Tuples. Just have a look below how to use tuple class if you want to create a new 8-tuple

var newTuple = new System.Tuple<string,int,int,int,int,int,int,int> ("Coding Defined",1,2,3,4,5,6,7);

So in the above example if you want to access any item you have to specify the item number as shown below :

newTuple.Item1 - 1st Item of the Tuple
newTuple.Item2 - 2nd Item of the Tuple
newTuple.Item3 - 3rd Item of the Tuple
and so on ...

This can be problematic if you do not know which item you have to pick basically when a tuple is passed as a function. There can be a scenario where you change the item order and then you have to figure out a new error all together. Along with this the concerns over memory allocation makes this Tuple class not so popular among C# Developers. But this is not the case with C#7. Lets take an example of C# Tuples

public (string value1, int value2, int value3, int value4) GetValues()
{
  return("Coding Defined", 1, 2, 3);
}

//Inside Other Function
var values = GetValues();
var firstValue = values.value1;
var secondValue = values.value2;
var thirdValue = values.value3;
var fourthValue = values.value4;

In the above example we have given a name to each parameter passed, but even if you do not give name you can access it using values.item1, values.item2 etc. Visual Studio provides you with both the name values and the order values, thus even if you forget the name you can access it via the order just like the System.Tuple class.


Where to Use Tuples


The best place to use tuples is when you are getting records or storing records from the database. This is usefuk because tuples can store a row no matter how many fields it has or the datatype each field possess. Other places to use tuples is to store them as global variables which can be used throughout your application thus you do not have to declare multiple constant values. The other places as shown above is to pass multiple values from a function. It can also help to remove hash table or dictionary which takes more memory.

Please Like and Share CodingDefined.com blog, if you find it interesting and helpful.

No comments:

Post a Comment