From the course: Learning C#

Returning multiple values - C# Tutorial

From the course: Learning C#

Returning multiple values

- [Instructor] As we saw in a previous video, sometimes it's desirable to return more than one value from a function. So the problem is that functions can only have one return value. So the C# language has something called an out keyword that lets you return values in normal function parameters. Now, unfortunately that sometimes leads to code that is hard to read, and it also kind of breaks the convention that parameters are used to give data to functions and return values are used to get results back. So starting back in C# version seven, Microsoft added support for a data type known as a tuple. A tuple is basically a lightweight data structure that lets you group multiple values together in one place. So let's open up our sample code here in the MultiValues folder in our functions folder, and let's take a look at a basic tuple. It looks something like this. So inside parentheses, which is what you use to group tuple values together. I can do something like this. I can declare int a, and int b and that becomes my type. So I'll use this type to declare a variable named tup1 and I'm going to set that equal to, and again in parentheses 5, 10. And I can do this other way too. I can also do it, just use the var keyword to make a tuple value like this. And then just our tup2 and then inside my parentheses, I can put a string and maybe I'll have like a floating point value. So in the first instance, I have a tuple with named elements a and b, and I can operate on them individually. So for example, I can do something like tup1.b = 20. In the second case, I didn't name the two elements but C# automatically names them for me as item one and item two. So I can do something like tup2.Item1 = "New String" and oh, let me put those down here in this section. So let's print these out. And we'll use a string interpolation for this. So I'll print out tup1.a and tup1.b and let me copy that and make another example. So we'll do tup2.Item1 and tup2.Item2. So let's go ahead and run this code and see what we have so far. Let's run this and then you can see the values of the tuples being printed out. So everything seems to be working. So what's nice about tuples is that they give us a very clean way of returning multiple values from a function. So let's rewrite our plus times function from the previous example to use tuples instead of out parameters. So I'm going to write, I'm going to declare the function as returning two ints and we're going to call it PlusTimes and it's going to take two integer arguments and it's going to return a tuple and the first value is going to be a+b, and the second value is going to be a*b. So now let's write the code to exercise this function. And once again, I'm going to get a tuple back with two integers. So I'll declare my variables as having two ints and I'll call that variable result and I'm going to call PlusTimes with six and 12 and they'll print the result. So we'll print out Sum and that's going to be result.Item1 and the product is going to be result.Item2. So now let's go ahead and run our updated code and we can see that the sum of six and 12 is 18 and the product is 72. So now we can see that the result we're returning has multiple values in it via tuples and we didn't have to resort to using the out keyword. So as I said previously, this is now the preferred way of returning multiple values from a function instead of using out parameters which you'll probably only see in older C# code but it's good to know how to do it both ways.

Contents