Coding Shorts: Tuples in C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to coding shorts with sean wildermuth today i want to tackle a topic called tuples now if you've come from other languages in c sharp you probably already know what tuples are and may be already comfortable with them but since i'm a long time c-sharp user i'm not as comfortable with them as i should be and so i want to talk through this small example of showing you some of the use cases of tuples to get your head around it and see how you can solve common problems with them let's get started [Music] so now let's dig into the idea of tuples i have an animal class that i'm just going to create a new animal with and what i'd like to have in that animal class is to get some result by asking animal get info right this doesn't exist yet so let's go ahead and generate that on animal and let's head over there so we're starting with this internal which i'll make public get info that's going to return something right obviously we're not going to throw an exception and what i'd like to do is be able to return a name and age right and so i'm going to come here and there's a few different ways i can do this i could do this with an anonymous object obviously so i could say name equals max and age equals 16 and this works but of course this is an anonymous object so we end up returning it as an object and how do we do something with it because when we look at the result what are we actually getting right getting an object back because there's no way to sort of talk about that type information and so when i have more than one piece of information that i want to share i'm gonna have to do something special with it and so what i usually do is create something like an animal info class right this is just a type that i can use often for this one-off sort of feature and then i can return something that i can actually get information from so that i can do console.writeline name is result.name h result.age right kind of a stupid program but you know we can start to see how it could be useful here but creating these one ops makes me kind of crazy and when c sharp seven came in we were able to do something different here we were able to do tuple and we could define that we need two pieces here we can say a string and let's say an integer right that we want to return those and then we could just create a new tuple string integer and just pass these back this could be a type that we could use sort of temporarily temporarily creating these types problem is name and age sort of disappear in those types we have result which is that tuple and what we can do here is just say item one and item two works not very elegant but it works right so this is a reference type that contains those pieces of information and the more you add you can sort of go add in for an item if i add more it's going to be item one two three you know up to 100 right to some hugely inaccessible number we want to go with a value type instead we can change the syntax to just parentheses and what this will do is allow us to send down just a parenthesis type here as long as it matches this type because obviously we couldn't say 16 here because it's assuming the second part of it is going to be a 16 right is going to be an integer and so our code here really doesn't change except we're getting an immutable version instead right still getting item one and two it's acting in fact the same way but you can actually name these so if i come in here and say name and age and notice that the syntax looks a lot like the parameter syntax right it's allowing you to specify the different names that are in there so it allows you to create them in order right so the construction of them is the same we're just adding name information with the types and so back down here instead of item one we can say name again and we can say age again in fact we look at that result the type information includes those parameters and so this is really useful when you don't want to have to add out parameters just because you want to return more than one piece of information you know because we could certainly have done this as getting through string name into age right so here we could set the age equals 16 and just return max and so this works we can do this all day long but i think the syntax of using a tuple just makes a ton more sense right that we're really giving that information back and one of the interesting things about the name syntax here is we can actually de-reference them so what if we wanted to use the result here we could actually come in here and say string name int age as the type now what does that actually do that creates two locals that dereference that information coming back and so that i can use these as whatever names i want in order to get those variables now you notice that this looks like it's one type but what it's actually saying is i create two variables here and the parentheses tell it that we want to dereference what is a tuple coming back now we are getting copies of these because they are new value types but it does simplify the sort of work we want to do here and notice that the name and age don't need to match the underlying type they just have to match the correct order so hopefully this has given you something to chew on about using tuples in your own code in simple ways to solve some real problems not having to worry about the semantics of tuples as much as seeing the utility of tuples in your own projects thanks for watching another coding shorts my name is sean wildemuth i'll see you next time you
Info
Channel: swildermuth
Views: 753
Rating: undefined out of 5
Keywords: Software Development, C#, Tuples
Id: eVNTDwr2_dk
Channel Id: undefined
Length: 6min 22sec (382 seconds)
Published: Sun Mar 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.