If youre creating an utility in C#, youll be used to calling functions to return specific values.

Sometimes, there may be cases where you may need to return multiple values from a single function.

There are different ways that you’re able to achieve this.

Code in VSCode on laptop sitting on the ground

One is to return several values inside a single array.

you’ve got the option to also assign values to multiple global variables, from inside the function.

What Is an Out Parameter?

Visual Studio Create a New Project

The standard way to return a value from a function is to use the “return” keyword.

it’s possible for you to also explore otherbasic programming principles.

Another way to retrieve a value from a function is to use an “out” parameter instead.

Create a new Console App project

you’re free to declare an out parameter within the signature of the method.

Instead of using the return keyword, you must always assign a value to the out parameter.

When you call the Calculation() method, pass in a variable for the out parameter.

Name and configure your new project

In this case, the value “30” will output.

you’re able to explore more about how to create and use classes in C#.

Generated start code for console app

Visual Studio runtime button

Result at Runtime for out parameters