How to use C# out parameter in programming?

In order to return multiple output values from a single function in C#, the out keyword is used. It makes able to a function to return multiple values in same time.
By: Steven Clark
Spread the Word
Listed Under

* C# out parameter
* C# output parameter

* C# Programming

* California City - California - US

Sept. 15, 2011 - PRLog -- A function can behave dynamically based on input parameter. There are various ways to pass parameter to a function and against the input parameter the function returns appropriate output. Function uses return keyword to returning value. This keyword can return only one value of same data type that is defined with function name. If you are needed to return multiple values from single function you will have to use output parameter in C#. In this C# tutorial, you will see how you can use output parameter in C #.

In order to use output parameter in C# function, you will have to use out keyword with variable name. It takes variable as output parameter and uses this variable to storing values returning from the function. For example, you have a function calculation that takes two integer values as input but returns four value as output like addition, subtraction, multiplication and division. In this situation the out keyword will use with variable name for assigning that the variable is output parameter.

Consider the following programming example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace out_parameter
   class Program
       public static void calculation(int num1, int num2, out int add, out int sub, out int mul, out float div)
           add = num1 + num2;
           sub = num1 - num2;
           mul = num1 * num2;
           div = num1 / num2;

       static void Main(string[] args)
           int addresult, subresult, mulresult;
           float divresult;
           Program.calculation(15, 5, out addresult, out subresult, out mulresult, out divresult);

           Console.WriteLine("Add = " + addresult);
           Console.WriteLine("Sub = " + subresult);
           Console.WriteLine("Mul = " + mulresult);
           Console.WriteLine("Div = " + divresult);



In the preceding example, we created a static function calculation that takes two input parameter as num1 and num2 and returns four out parameter as out int add, out int sub, out int mul, out float div. In the Main() function we call this function and store the output in four different variable as addresult, subresult, mulresult and divresult.
It is very easy to use out parameter in C# programming. It is also very important function that makes your program easier and simpler. You don’t need to create multiple functions for doing same group of task. Just create single function and return number of value as you want.

For more info about C# out parameter, please visit to:
Email:*** Email Verified
Tags:C# out parameter, C# output parameter
Industry:C# Programming
Location:California City - California - United States
Account Email Address Verified     Account Phone Number Verified     Disclaimer     Report Abuse
Csharp Tutorial News
Most Viewed
Daily News

Like PRLog?
Click to Share