C#: output parameters

Typically, you can only return one thing in C#. For instance, you may return an int, a string or an array of bools. As you may have surmised, output parameters allow you to return multiple values without using a container.

Example I:

Let's take a look at a simple method:

private void Subtract(int x, int y, out int result) 
{
    result = x - y;
}

Figure 1: a method with an output parameter

We're subtracting y from x and storing the answer in our result variable. We've added the out modifier to our result varible to signify it is an output parameter. Let's see what this means:

Subtract(10, 4, out int result);
Console.WriteLine("{0} - {1} = {2}", 10, 4, result);

Figure 2: calling our output parameter method

Passing in 10 and 4 as the first two arguments to Subtract should not throw you off. You may be wondering about our little friend result. What's happening here is that in Subtract, we're assigning a value to result. When we call Console.WriteLine, we can pass in result as the third argument and we get our expected result: 6.

This is the output you get if you run this:

10 - 4 = 6

Figure 3: output of calling Subtract()

One thing to note before we move in is that we had to specify the out modifier in the parameter list of Subtract (figure 1) and in the argument list when calling Subtract (figure 2). I didn't initialise result before invoking Subtract, but this requires C# 7 or later.

Some of you may be wondering what would happen if we hadn't assigned result in Subtract. Would we get a compiler error in Console.WriteLine() since we hadn't assigned a value or would the compiler complain about us not setting a value for result in Subtract? It's the latter: the compiler requires us to assign a value in Subtract. You have to assign a value to it before using it.

But why?

Great, but why on earth would you want to do that? You wouldn't, this is just a toy example, but that's not to say that output parameters aren't useful – they can be. They can be useful in places where you may want to return multiple things.

Example II: returning multiple output parameters

Let's now take a look at a method that returns multiple output parameters:

private void Example2(out string name, out int age, out bool isHungry)
{
	name = "Clark";
    age = 30;
    isHungry = true;
}

Figure 4

In this example, all three parameters are output parameters. That means that all three will be returned.

string firstName = "Kent";
int age = 20;
bool isHungry = false;

Console.WriteLine("Before: {0} is {1} years old and is {2} hungry? {2}", 
	firstName, age, (areTheyHungry ? "" : "not ")); 
Example2(out firstName, out age, out isHungry);
Console.WriteLine("After: {0} is {1} years old and is {2} hungry? {2}", 
	firstName, age, (areTheyHungry ? "" : "not ")); 

Figure 5

And the output:

Before: Kent is 20 years old and is not hungry
After: Clark is 30 years old and is hungry

Figure 6

As you can see from figure 5, the name I gave to the string (firstName) did not need to match the name of the parameter in Example2 (name). The same goes for any of the other parameters: I could have given age and isHungry different names to the names of the parameters in Example2.

Figure 5 also shows you that the out variables can already have been assigned to values.

Summary

With an array, all of the items in it have to be of the same type. Here, we have seen how to return multiple values from a method. There are other ways to do this (e.g. using an object), but you may find this to be useful for your specific use case.

Note that tuples (the ValueTuple data type) is a container that can store values of different data types. However, it would be wrong to necessarily think of tuples as an alternative to output parameter (although you may be able to use a tuple in place of output parameters). As since a tuple itself can be an ouput parameters. As such, should you want to, you could have multiple tuple output parameters.