Intro to Tuples in C# In 10 Minutes or Less

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's look at what tuples are in c sharp now for most of my training i work to give you an in-depth perspective on technology but sometimes you just need to get the quick answer to the question how do i use this so that's why i created the 10 minute training series so let's dive right into this code now here i have a little bit of code set up in visual studio 2022 in a console application so what i have here is this method called validate address now of course this is all sample data and sample code but essentially you pass in a string here and what i want to happen is not complete yet what i want to happen is if the address equals one two three sesame st then i wanna return one two three sesame street like that and say yes that's valid but if it's not this then i want to say no that's an invalid address okay so it's a very rudimentary sample of how you do validation where you really want to return more than one value so this validation for validate address i want to return the validated properly formatted street address okay that's a pretty common thing for addresses where you you type in a street address however you know you abbreviate or whatever and then it goes did you mean this and it creates the the nicer format and it you know you have a better address because it's then standardized for what the postal system is expecting so we want that we wanted to format it pretty but we also want to make sure that it's a valid address too because if i put the address of one well that's not a valid address therefore should return back and say that's not a valid address not validate the address by making it formatted correctly you can't format correctly an invalid address so i want to return two pieces of data here now one way that we used to do this is we'd create a class where we create class that's just basically for returning information from validate address and that's kind of messy the other way to do it would be to make this an out variable where what it would do is um it would change this so that it not only sends it in but also sends the data back out and again that gets kind of messy because now you've got parameters in line that are also returning uh values uh it's actually i think out ref uh i haven't done this in a while um but anyways uh we're not gonna do that because we now have tuples we don't have to do this tuples the idea is instead of creating a class just for one method to return two pieces of data or more you can do three four five but or more but the idea is instead of creating a class just for that one scenario and then it's clogging up your your project a whole bunch of classes that are only used in one place for one use we can just create a return type that has more than one parameter in it so what we're going to do is set this up so that we have more than one parameter being returned the way you do that is by using parentheses and it can say i want to return both a string and a boolean now i'm not a big fan of this but this does work and the reason i'm not a big fan of it is because yes you could do that but then they're not named so it's just it's going to be type 1 and type 2 i think so if we were to grab this here var result equals and then look at result dot it's item one so item one item two well what does item one represent i don't know this is why you don't name it that way okay instead what you say is uh string address and then bool is going to be is valid and now when you see the result you can say dot address and is valid so you have both of those on that value so now we can do is we can say if address equals one two three sesame street we're going to return and put in parentheses here return this and true meaning it's both valid and here is the correct address for this one we're going to do is we're going to return and we can return the same address and false if we want to that way they know what was invalid well it was the address you passed in so you can do that if we want so with that we now have this method returning a tuple again you can put comma and put more types here if you want if you get too crazy that's a little over the top maybe you should use a class but for things where it's just a one-off kind of thing this makes a lot of sense and now we have var results or i could say i could actually you know name this the tuple we don't have to because it already knows what that type is and that's the the whole tuple which has both values inside of it like we saw so you say result dot address and we can say result dot is valid okay so with that we can enable the rest of this where we say result dot is valid if result is valid then print out let's put our string interpolation here and say result dot met or address and then down here we can say it's an invalid address if we run this now we're gonna see that your validated address is one two three sesame street and if we change this to let's just knock off the one the beginning so it's not the incorrect address it's going to say that's an invalid address so it knows both values being returned but we could also go one step further and say hey what if we want to knock this out into two variables instead well we can do that by saying string message string is or let's just say valid i know it should be is valid probably um because the fact that oops not string bool it knew that i forgot it because the fact that i just want to show that you can rename these um so message isn't the same as address valid isn't the same but that will allow you to create independent variables like this and that will still work so you can name them whatever you want now this is called a deconstructor not a destructor that's different this is a deconstructor which deconstructs our tuple back into separate parts okay now the cool thing is maybe you don't care about all the parts for every return maybe you just want the the validated address and you don't really care about is valid or whatever so in that case let's just do this again down below because i don't want to mess up this code right here but let's let's uh comment it out so you have it for later if you want this code by the way if you want the code it's down in the description but we could say we could use a discard character which is an underscore we're basically saying i don't care about if it's valid or not i just want the resulting message or address okay that discard character says i don't care about that part of it just give me the part i do care about so we can also use the discard character for things that maybe don't want in every situation or calling this method maybe sometimes we just want the validated address and don't care if it's valid or not we just want it formatted properly if we can okay well that's that would be how we do it using the discard character which ignores that variable part of the tuple being returned okay so that's tuples they're primarily useful for returning multiple parameters from a method that's the the big deal about tuples um you can use other circumstances but really it comes down to that's the important one that we use it for all right let me know what your thoughts are with tuples do you use them do you not use them have you even seen it before let me know down in the comments i appreciate a little bit interaction let me know what you found valuable thanks for watching and as always i am tim cory [Music] [Applause] you
Info
Channel: IAmTimCorey
Views: 13,615
Rating: undefined out of 5
Keywords: .net, .net tutorial, 10-minute training, c#, c# tutorial, iamtimcorey, tim corey, tuples, c# tuples
Id: QC6hpl2iU0c
Channel Id: undefined
Length: 9min 50sec (590 seconds)
Published: Wed Nov 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.