Out Parameters In C# 7.0

Introduction

In this article, we will discuss a new way of declaring and initializing the out parameters in C# 7.0. This article can be used by beginners, intermediate, and professionals.

We are going to cover:

  1. What is the Out parameter?
  2. Scope of Out parameter.
  3. How to ignore the Out parameter?
  4. Out parameter with Var datatype.
  5. Out parameter with TryParse.
  6. Important points of Out parameter.

What is Out parameter?

It is normally used when the method wants to return more than one value. It is a keyword in C# that is used to pass as an argument to a method as a reference type to get value from the method.

Before C# 7.0, if we want to pass the out parameter to the method, we need to declare the variable first and then can pass it into the method. See below code before C# 7.0.

using System;  
  
namespace OutVarible  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            int sum, sub;  
            double mul, div;  
            Math(5, 10, out sum, out sub, out mul, out div);  
            Console.WriteLine("Sum : {0}, Sub :{1} , Mul :{2}, Div : {3}", sum, sub, mul, div);  
            Console.ReadLine();  
        }  
  
        public static void Math(int a, int b , out int sum, out int sub, out double mul, out double div)  
        {  
            sum = a + b;  
            sub = b - a;  
            mul = a * b;  
            div = b / a;  
        }  
    }  
}

The output of the above code,

The new way of declaring and using out parameter in C# 7.0 is below,

using System;  
  
namespace OutVarible  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Math(5, 10, out int sum, out int sub, out double mul, out double div);  
            Console.WriteLine("Sum : {0}, Sub :{1} , Mul :{2}, Div : {3}", sum, sub, mul, div);  
            Console.ReadLine();  
        }  
  
        public static void Math(int a, int b , out int sum, out int sub, out double mul, out double div)  
        {  
            sum = a + b;  
            sub = b - a;  
            mul = a * b;  
            div = b / a;  
        }  
    }  
}

Output

Based on the above code, I can say that we can pass the out parameter to the method without declaring it.

Scope of the Out parameter

See below code to understand the scope of the out parameter, 

using System;  
  
namespace OutVarible  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            string str = "Kirtesh";  
            if(int.TryParse(str,out int i))  
            {  
                Console.WriteLine(i);  
            }  
            Console.WriteLine(i);  
            Console.ReadLine();  
        }  
    }  
}

In the above code, int variable "i" is declared in the if block. But we can see that variable "i" can be used outside of the if block.

How to ignore the Out parameter?

Sometimes we need to ignore out parameters. Let's try to understand this concept using the above Math method. Suppose we want only Sum, Sub, and Mul but don’t want Div, in this case, we will pass out with underscore(out _) in place of Div out parameter.

Let's see the below code,

using System;  
  
namespace OutVarible  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Math(5, 10, out int sum, out int sub, out double mul, out _);  
            Console.WriteLine("Sum : {0}, Sub :{1} , Mul :{2}", sum, sub, mul);  
            Console.ReadLine();  
              
        }  
  
        public static void Math(int a, int b , out int sum, out int sub, out double mul, out double div)  
        {  
            sum = a + b;  
            sub = b - a;  
            mul = a * b;  
            div = b / a;  
        }  
    }  
}

Output 

Out Parameter with Var data type

We can assign a var data type directly to the argument of the out parameter.

The below code explain the same.

using System;  
  
namespace OutVarible  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Math(5, 10, out var sum, out var sub, out var mul, out var div);  
            Console.WriteLine("Sum : {0}, Sub :{1} , Mul :{2}, Div : {3}", sum, sub, mul,div);  
            Console.ReadLine();  
              
        }  
  
        public static void Math(int a, int b , out int sum, out int sub, out double mul, out double div)  
        {  
            sum = a + b;  
            sub = b - a;  
            mul = a * b;  
            div = b / a;  
        }  
    }  
}  

Output 

In the above code, we have replaced int and double datatype with Var and it is working fine.

Out parameter with TryParse

Please see the below code,

using System;  
  
namespace OutVarible  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            string str = "22-Dec-1980";  
            if(DateTime.TryParse(str,out DateTime dt))  
            {  
                Console.WriteLine(dt);  
            }  
            Console.ReadLine();  
        }  
    }     
}

In the above code, Date parse successfully and we will get the below output.

Output

What will happen if TryParse will be failed, let’s see the below code to understand this scenario,

Suppose we have string, and we are trying to parse as int. In this case, it will fail and an exception occurred. In case of an exception, it will return the default value of the datatype. In our case it is integer hence value will be Zero. 

using System;  
  
namespace OutVarible  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            string str = "Kirtesh";  
            if(int.TryParse(str,out int i))  
            {  
                Console.WriteLine(i);  
            }  
            Console.WriteLine(i);  
            Console.ReadLine();  
        }  
}  
} 

Output

Important points

Below are a few of the important points of the out parameter,

  1. The main benefit of the out parameter is, code will be more compact, Readable, and easy to understand.
  2. The out parameter can be used with generic methods.
  3. The out parameter can not be used with the Async method.
  4. It is not allowed to use the out parameter with iterator methods.
  5. Can not be used with properties.
  6. Method overloading can be done using out parameters.
  7. In the extension method, the first argument cannot be the out parameter.
  8. In the same block only, Inline out parameters can be access

That is all for this article. Hope you enjoyed and learned from this article.

To know more about Out parameter, follow below articles


Similar Articles