C# provides special parameter types that allow methods to return additional information to the caller. One of these is the out parameter, which is used to pass values by reference.
In this chapter, you will learn what an out parameter is, its syntax, how it works, and how to use it in C# with examples.
The out parameter is used to pass an argument by reference and return a value from a method. Unlike a normal variable, an out parameter does not need to be initialized before it is passed to the method. However, the called method must assign a value to the out parameter before it returns.
The out keyword is commonly used when a method needs to return multiple values.
It has the following syntax:
Let us take an example to illustrate the out parameter in C#.
Output:
The Square of 6 is: 36
Explanation:
In this example, we have taken the out parameter to calculate the square of a number. After that, we define a method named GetSquare that takes an integer input and uses an out parameter to return its square. In the main method, we define the number and call the GetSquare method. Finally, we print the square of a number using Console.WriteLine() function.
Several important points of out parameter in C# are as follows:
Let us take an example to illustrate an out parameter in C#.
Output:
The Value set by the method: 10
Explanation:
In this example, we create a method named SetValue that accepts one out parameter called number. Inside the method, we assign the value 10 to this parameter. In the Main method, we declare a variable result without initializing it. After that, we create an object of the program class and call the SetValue method using the out keyword. This allows the method to set the value of the result.
In C# programming, the out parameter is used when we need to return more than one value from the method. Let's take an example of two numbers from a single method call.
Output:
Sum: 15, Product: 50
Explanation:
In this example, we create a method named SumAndProduct that accepts two out parameters. Inside this method, we calculate the sum and product of the numbers. In the main method, we declare that the (5 and 10) values are passed to the method and return their sum and product of the number.
In C#, the multiple out parameter allows passing multiple out parameters to the method, and the method returns multiple values.
Syntax
It has the following syntax.
In this syntax,
Let us take an example to illustrate the multiple parameters in C#.
Output:
Name: Alice Age: 25 City: New York
Explanation:
In this example, we create a method named PrintDetails that accepts three parameters: a name, an age, and a city. Next, we use the Console.WriteLine() function to print these values. After that, we have call the methods(PrintDetails) in the main method by passing the values "Alice", 25, and "New York".
Several differences between the ref and out keywords are as follows:
| ref | out |
|---|---|
| It is mandatory to initialize the parameter before it is passed to the ref. | It is not necessary to initialize the parameter before it is passed to out. |
| It does not need to initialize the value of the parameter. | It is necessary to initialize the value of the parameter. |
| The data is passed in a bi-directional(in/out). | The data is passed in unidirectional(out). |
| It is used for modifying the existing value. | It is used for returning a new value. |
We request you to subscribe our newsletter for upcoming updates.