c sharp

C# Out Parameter

The out parameter in the C# programming language will be discussed in this article. The out Parameter is a reference parameter that does not require predefined variable declaration and can be called with the same parent variable or object name. The out parameter is mostly used when a method has more than one parameter to return when it is called. The out keyword is used before passing an argument in the parameter brackets. The out parameter references itself to the predefined variable that is either called in the main function or in the method itself.

Example 01: Returning One Parameter From a Method Using the Out Parameter in Ubuntu 20.04

In this example, we will be creating a method in which we will return a single parameter by doing some calculations on it; the parameter will be passed by using the out parameter in the C# programming language. The parameter that will be returned would have the same name as the reference variable. The Out parameter then will be called in the main function and be displayed in the output. We will be implementing this example on the Ubuntu 20.04 environment.

Graphical user interface, text Description automatically generated

We created a method called “multiply”, in which we defined a variable and then multiplied it by itself. This variable is returned as an out parameter from the method, which is then called in the main function and printed in the output using the WriteLine() function.

Text Description automatically generated

After compiling the above code on our command line terminal, we displayed the output, which is the product of the given variable by itself; this proves that the Out parameter was returned successfully.

Example 02: Returning Two Parameters From a Method Using the Out Parameter in Ubuntu 20.04

In this example, we will be creating a method from which we will be returning two parameters from that method. Both the parameters will be returned after some calculations are done in the method and will be called in the main function with the support of the Out parameter, where they will be displayed on the output screen. This C# program will run in a “.cs” file that will run under the Ubuntu 20.04 environment.

We have developed a function called “Double”, in which we have initialized two variables and then multiplied them by two in the code above. After this, these two variables are called in the main function with the help of the out parameter. As the variable values are doubled, they are passed into a print function to be displayed in the output.

Text Description automatically generated with low confidence

When the above code is compiled and executed on the command line terminal, this is the output that we will get. In the output, the values of both the variables “a” and “b” are multiplied by two and then displayed on the output screen. The multiplication of these values shows that the Out parameter returned the correct values.

Example 03: Returning a Modified Parameter From a Method Using the Out Parameter in Ubuntu 20.04

In this example, we will be creating a method that will modify the value of the variable that would be returned using the out parameter in the C# programming language. In the main function, we would first display the original value of the variable, and then by using the method, we will call the modified value and then display it to visualize the difference that the out parameter created by using the ubuntu 20.04 command line terminal.
Text, letter Description automatically generated

In the above code, we created a method called “modify”, in which we declared a variable and then modified it and returned the modified variable to the main function. In the main function, first, we create an object of the class and then initialize the variable that will be modified in the method. After compiling and executing the above C# program, we will get the following output:

Text Description automatically generated

As we can see in the output that the first value of variable “a” is different after we modified it using the out parameter; even though we used different names for the out parameter and the variable, the compiler was able to reference both of the variable and parameter, and that is why we have successfully modified the value of the variable “a”.

Example 04: Returning Three Parameters From a Method Using the Out Parameter in Ubuntu 20.04

As we have successfully returned two parameters in the above examples, now we will focus on more complex returns. In this example, we will be returning three parameters from a method by using the out parameter in the C# programming language. Three variables of various data kinds will be saved in the procedure that will be created. All these three variables will be initialized in the main function and then be called the main function to be displayed on the output screen with the help of the Out parameter.

Text Description automatically generated

In the above code, we made a method in which the details of a person are mentioned in three different variables, which are “age”, “name”, and “status”. In the main function, we will initialize these variables and then create an object of the class that will help us call the method we created. After calling the method, we will get the values of the variable and then display them on the output screen due to the help of the Out parameter.

Text Description automatically generated

After compiling the above code, this is the output that we would get. In the output, we can see the detail of a person displayed in three categories: name, age, and status. All these three values were returned due to the Out parameter.

Example 05: Calculating the Area of a Triangle From a Method Using the Out Parameter in Ubuntu 20.04

In the following example, we will calculate the area of a triangle by a method in which the area would act as a variable, and its value would be returned with the help of the out parameter in a C# program. The formula for calculating the area of a triangle will be stored in the “area” variable in the method, and it will be returned to the main function to be presented on the output screen.

Text Description automatically generated

In the above C# Program, we created a method named “aot”, which has three parameters. Two of them are normal, and the third one is an out parameter. The area variable in the method is multiplying both the other variables and dividing their product by two to get the area of a triangle. In the end, we printed the area on the display screen that we got from the out parameter.

Graphical user interface, text Description automatically generated with medium confidence

This is the result after compiling and executing the above code. As the output suggests that the area of the triangle is calculated accurately and returned successfully from the method because of the Out parameter.

Conclusion

In this article, we discussed a special type of parameter in the C# programming language: the Out parameter. The out parameter does not require initializing and can be returned with a reference pointer. We also implemented several examples of the out parameter in the C# programming language in the ubuntu 20.04 environment, which gave us a better grasp of this special type of parameter.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.