Tuples in C#.Net made easy! | Generic Tuple | Value Tuple | Named Tuple | Codelligent

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome to another video of core Allegiant in this video we will try to understand what are tuples in C sham dotnet these are topics where which we will be looking into first we will see what exactly is a tuple class or a generic tuple class which is available in dotnet framework from a long time and then we will see what our value tuples which has been recently introduced and then we will look into named tuples and finally we will look into a demo wherein we can use tuples in different scenarios first let's try to understand what is generic double class a tuple is actually a data structure which can contain data of different types so if at all you have a requirement wherein you want to pass multiple data types values together you can use a table so you can see on the right side this is the way we can create a tuple so you can use the keyword tuple and in the angular braces which you use normally for generics you can use different data types so I have used in strength you can use many more if you put comma so the maximum possible is 8 values and you can initialize this variable by saying new tuple of int comma string and in the braces you can pass the values or there is a static method which double class provides so you can say double dot create and just pass the values so based on the data type of the value which you use internally it will decide what type of double object it is so in this I have used one integer and 1 string so internally it creates this obj similar to the above one with an int and string and if you want to use these values later in the code you can always use the object dot item1 item2 so if you are having more than two you will get the options like a b obj dot item 3 item 4 and so on so as i said it can take up to eight values so if you need more than eight values there is an option wherein you can nest different tuples and you can make it more than eight values but one thing which you need to keep in mind it's a reference type so because of which it was having a performance issue and then Microsoft has introduced value tuples so the value tuple is similar to the tuple class but it's an underlying concept is not the tuple class it's a value tuple struct and this can take more than eight values the way you use it is different you don't use the tuple class or the tuple keyword instead you can directly say where any variable is equal to put the values in the braces so I have used 1 2 3 comma ABC I can use comma 1 comma 2 so I can just go on so it can take as many numbers as many values as possible and then you can use them as obj dot item 1 item 2 item 3 etc so we will see in the demo section of how we can use these types the very end you will get more clarity about it but here if you can see you have a very big disadvantage that is like whenever you use some variable like for example if I save we're employee equal to 1 to 3 comma ABC where I think 1 2 3 is the employee ID and ABC is the employee name but when you are using this variable later in the code you will have to reference it saying obj dot item 1 OB 0 item 2 but at that time you don't know what is item 1 and what is item 2 so the values hold no information so to overcome this there is a new thing which is introduced that is named tuples so named tuples is same as value tupple the underlying stuff is the value tuple structure itself not the class but it has an option wherein you can name these values so the same way how you are shown in your previous code I can say where obj and I can define my type that is EMP ID : the value then EMP name : the value so now instead of using obj dot item 1 and item 2 I can use these names which I have defined here so these names you need not have to create a variable of this name or you need not create a separate class with some property or anything it's just dynamic and this has been introduced recently in c-sharp 7.1 so now let's jump into Visual Studio and see how we can use them I am in Visual Studio and I have created a small console application which has a main method so first I'll show you how to use the tupple class so when you say tupple you can see there is a class called tupple and you can put the angular braces you can say int comma string comma double so whichever data types you want to use you can use I am using three variables now and then I can say some may be obj or whatever variable you want to name it equal to new double and then I can just pass the value so I will say 1 2 1 comma some name like maybe Thomas comma some double value maybe 30,000 and that's it so your couple is ready so whenever you want to use it you can say obj dot you can see there is item 1 item 2 and item 3 so anytime you can get these values or if you don't want to use this way you can say where EMP equal to use the tupple class and there is a create method so you can say create and you can say 1 2 1 comma the thomas comma some value so this way also you can create and again you can use it EMP dot item 1 item 2 and item 3 so these two are nothing but similar the only difference is you have create the instances by using the triple class and instantiate instantiating it whereas here the tupple class exposes a static method to create and ultimately you return the same tupple object so let's see an example where we can use it for this way of using Topol is very much helpful when you are dealing with some methods so for example I have some method light public static maybe void get employee details so imagine that this method has lot of functionalities wherein it is fact fetching it from maybe database or wherever like there is a lot of functionalities involved in this but ultimately at the end you want to return some data related to employee so if I use it I can return int I can return the employee ID however I can use string to return the employee name but you want to return the employee ID name and double together so how will return it so one option is maybe you can use the out parameters or another option is you can create a class of employee type and instantiate the object here and then return it or if you don't want to take all those pains it is like just one time usage then instead of creating a class and all those stuff you can just use double so instead of string you will return double int commas string comma double so now whenever I want to return I can say return new tuple and I can pass one two two one that is my employee ID and then some name maybe like John and the salary so maybe like 30,000 and I can use it here in the method like where EMP equal to get employee details and I will just print it console dot write line I will say maybe EMP ID and I will use EMP dot now you know that the item one is Bridey item 2 is the name and item 3 is the salary so I'll say item 1 comma I will say the name so that is e MP dot item 2 has salary maybe I'll say rupees and I will say here T MP dot item 3 so now when you run this you get the values from that method so you can see MP ID 1 2 2 1 John has salary 30 30,000 now instead of this in the new C shop you can use the value type so what you have to do is just replace this tuple with brackets so instead of using the tupple class we are using the value type and when you are returning no need of creating the new instantiation of class and all just return this values in the brackets so now again this holds good the same way now if you run it it remains the same but the only disadvantage here is you don't know what exactly is item 1 item 2 item 3 so later in the if we are this is a simple application so if your application is very complex and it has lot of code then by the end you use this EMP variable you might not know what is a tough one what is Adam - or what is Adam say to make it better you can use it the named values so to use to change this method into named tuple you can define here int you can say EMP ID string EMP name and double salary so this is how you can use the named ones so now instead of item 1 now when you press dot you can see you have ibid EMP name and salary so I can use the EMP ID and dot EMP name and dot salary so now the code looks more readable and you will be able to use it in your code wherever you want now you might be wondering like is it like we have to create the tupple only at the returned it is not like that maybe I can do something like where TM equal to I will instantiate with some default values there zero and some string dot empty comma sum 0 so this is my initial value which have created now suppose there is there are lot of functionalities here so a lot of codes and after that I will get the employee ID so I can say yeah so again here it will be item 1 item 2 3 so instead of that I will say here ename sorry e ID so that I can identify what exactly is those values I'll say a name and finally I will say salary so now wherever I want I can say am e/m dot e ID and I can assign the value and then lot of code and finally when I get my name so I can say e m dot e name is equal to maybe something Lucy and then again after lot of code maybe I can I will have to calculate the salary so maybe I will use double Sal equal to maybe like 3000 into 12 or this is just a simple functionality but maybe you have a complex logic to calculate the salary using the basic salary HR a and many other components so you can have all those functionalities here and finally you get the salary then what I will do is I will say am dot salary equal to I will assign this variable itself and finally instead of returning this I will say return p.m. so now I think okay sorry i have used salary as int instead of that I should be using double so now it will not give that error yeah so now I am returning this IAM so here you will notice like the name which I am using here is EA ID ename but the return I am using EMP ID EMP name salary so it doesn't matter if the data type is remains same it will just type cut into it so internally when I am using it is all eme IDE m dot e name but once I return from this method it will be e MP dot EMP ID MP dot EMP name so these well names will be reflected outside so now when you run this the it still works so it is the same way how it is so this using of tupple is very helpful so whenever you want to just instantiates a set of values you want to pass it from a method or something or another very good example where you will require it is like for example you want to create a rectangle so you are dealing with some graphics or something and you just want to say the length is some value like 3 and your red is some 5 so now wherever you are using you will say rectangle dot you get the length and breadth similarly you can have like for example you are using points maybe in a graphics application you are using points so you can say point 1 X as some 4 and Y as some 5 so now wherever you are using this point you can say Oh point 1 dot X & Y so this way it is very helpful whenever you have to pass some set of values you don't have to create any class and properties etc you can just instantiate everything like this and just pass the value I hope this is useful so whenever you are coding next time and you are dealing with some set of values try to use tuple and you will feel that it is much easier than creating any class or strut so if you like this video please throughout the channel and like the video and if you need any more information do recommend it below the video I will try to answer them there itself thank you
Info
Channel: CODELLIGENT
Views: 2,804
Rating: undefined out of 5
Keywords: C#, .Net, Tuples
Id: bDVQ83AbaaM
Channel Id: undefined
Length: 15min 32sec (932 seconds)
Published: Sat Mar 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.