C# Out Parameter with Examples

In c#, out keyword is used to pass an arguments to the method as a reference type. The out keyword is same like ref keyword, only difference is out doesn’t require a variable to be initialized before we pass it as an argument to the method but the variable must be initialized in called method before it return a value to the calling method.

 

The out parameter in c# is also useful to return more than one value from the methods in c# programming language.

Declaration of C# Out Parameter

Following is simple example of using out parameters in c# programming language.

 

int x; // No need to initialize variable

Multiplication(out x);

If you observe above declaration, we just declared a variable x and pass it to the method by using out parameter without assigning any value but as discussed, the variable must be initialized in called method before it return a value to the calling method.

 

To use out parameter in c# application, both the method definition and the calling method must explicitly use the out keyword.

C# Out Parameter Example

Following is the example of passing an out parameter to the method in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            int x;

            Multiplication(out x);

            Console.WriteLine("Variable Value: {0}", x);

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

        public static void Multiplication(out int a)

        {

            a = 10;

            a *= a;

        }

    }

}

If you observe above example, we declared a variable x and passing it to a Multiplication method by using out keyword without initializing the value. However, the called method (Multiplication) is initializing the value before it returning the value to the calling method. 

 

When we execute above c# program, we will get the result like as shown below.

 

C# Out Parameter Example Result

 

If you observe above result, the changes whatever we did for variable in Multiplication method has reflected in calling method also.

C# Multiple Out Parameters Example

Following is the example of using multiple out parameters in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            int x, y;

            Multiplication(out x, out y);

            Console.WriteLine("x Value: {0}", x);

            Console.WriteLine("y Value: {0}", y);

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

        public static void Multiplication(out int a, out int b)

        {

            a = 10;

            b = 5;

            a *= a;

            b *= b;

        }

    }

}

If you observe above example, we defined a two variables (x, y) and passing it to Multiplication method using out parameters.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Multiple Out Parameters Example Result

 

If you observe above result, the changes whatever we did for variables in Multiplication method has reflected in calling method also.

 

This is how we can use out parameter in c# programming language to pass an arguments to the method as a reference type in c# programming language based on our requirements.