Out Variables in C#

Out Variables in C# 7 with examples

In this article, I am going to discuss the new Out variables in C# with some examples. With the introduction of C# 7, now it is possible to define the method’s out parameters directly within the method.

Before C# 7, it is a little difficult task to work with the “out” parameter. We need to split their declaration and usage into two parts i.e. first we need to declare a variable and then we need to pass it to the method using the out keyword.

Note: The most important point that you need to remember is, unlike ref, the out parameter it does not require to be initialized while declaring as these variables are going to be overwritten by the method. Again you cannot use the “var” data type to declare these variables.

Example1: Using Method Parameter

Let us understand the Out variables in C# 7 with one example.

Before C# 7, we use the keyword out to pass a method argument’s reference. If you want to use an out parameter, then you need to explicitly specify the keyword out in both the calling method and method definition. The following example defines the GetEmployeeDetails() method with four out parameters.

class Program
{
    static void Main()
    {
        string EmployeeName, Gender, Department;
        long Salary;
        GetEmployeeDetails(out EmployeeName, out Gender, out Salary, out Department);
        Console.WriteLine("Employee Details:");
        Console.WriteLine("Name: {0}, Gender: {1}, Salary: {2}, Department: {3}",
        EmployeeName, Gender, Salary, Department);

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }

    static void GetEmployeeDetails(out string EmployeeName, out string Gender, out long Salary, out string Department)
    {
        EmployeeName = "Pranaya Rout";
        Gender = "Male";
        Salary = 20000;
        Department = "IT";
    }
}
OUTPUT:

OUT variables in C# 7

 

 

But with the introduction of C# 7, now you can define a method’s out parameters directly within the method. So the above program can be rewritten as shown below and also gives the same output.

class Program
{
    static void Main()
    {
        GetEmployeeDetails(out string EmployeeName, out string Gender, out long Salary, out string Department);
        Console.WriteLine("Employee Details:");
        Console.WriteLine("Name: {0}, Gender: {1}, Salary: {2}, Department: {3}",
        EmployeeName, Gender, Salary, Department);

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }

    static void GetEmployeeDetails(out string EmployeeName, out string Gender, out long Salary, out string Department)
    {
        EmployeeName = "Pranaya Rout";
        Gender = "Male";
        Salary = 20000;
        Department = "IT";
    }
}

Run the application and you will get the output as expected as shown in the below image.

C# OUT variables

 

 

In the above program, the out variables are in the scope of the enclosing block. So that the subsequent line can use them. As the out variables are directly declared as arguments to the out parameters, the compiler can easily tell what their type should be. So it is always better to use the “var” data type to declare them as shown in the following example.

class Program
{
    static void Main()
    {
        GetEmployeeDetails(out var EmployeeName, out var Gender, out var Salary, out var Department);
        Console.WriteLine("Employee Details:");
        Console.WriteLine("Name: {0}, Gender: {1}, Salary: {2}, Department: {3}",
        EmployeeName, Gender, Salary, Department);

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }

    static void GetEmployeeDetails(out string EmployeeName, out string Gender, out long Salary, out string Department)
    {
        EmployeeName = "Pranaya Rout";
        Gender = "Male";
        Salary = 20000;
        Department = "IT";
    }
}

Run the application and you will get the output as expected as shown below.

Out variables

Wildcard out variables in C#

The .NET Framework provides a wildcard called underscore (‘_’) as the name of a parameter if you want to ignore an out parameter of a method. For example, if you don’t care about the Department parameter, then you could just replace it with an underscore (‘_’) as shown below.

class Program
{
    static void Main()
    {
        GetEmployeeDetails(out var EmployeeName, out var Gender, out var Salary, out _);
        Console.WriteLine("Employee Details:");
        Console.WriteLine("Name: {0}, Gender: {1}, Salary: {2}",
        EmployeeName, Gender, Salary);
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }

    static void GetEmployeeDetails(out string EmployeeName, out string Gender, out long Salary, out string Department)
    {
        EmployeeName = "Pranaya Rout";
        Gender = "Male";
        Salary = 20000;
        Department = "IT";
    }
}

OUTPUT:

Out Parameters in C#

Out Parameter Using TryParse

When you are working with real-time applications, then the common use of out variable is the Try… pattern, where a boolean return value indicates the success, and if successful then the out parameters carry the results.

Let us understand this with an example.

Before C# 7

class Program
{
    static void Main()
    {
        string s = "09-Jun-2018";
        DateTime date;
        if (DateTime.TryParse(s, out date))
        {
            Console.WriteLine(date);
        }

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
OUTPUT:

C# out Parameters

 

 

With C# 7, the above code can be rewritten as shown below

class Program
{
    static void Main()
    {
        string s = "09-Jun-2018";
        if (DateTime.TryParse(s, out DateTime date))
        {
            Console.WriteLine(date);
        }
        Console.WriteLine(date);
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

OUTPUT:

OUT variables in C# 7

 

 

In the above example, as you can see, we are declaring the out variable within the method and it is being accessed from outside also. If an exception has occurred, then the out variable will be assigned with a default value. Let’s see this with an example.

class Program
{
    static void Main()
    {
        string s = "09-Junnnneee-2018";
        if (DateTime.TryParse(s, out DateTime date))
        {
            Console.WriteLine(date);
        }
        Console.WriteLine(date);
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

OUTPUT:

Out Parameters Example in C#

 

 

In the next article, I am going to discuss one more interesting new feature of C# 7 i.e. Pattern Matching with an example.

SUMMARY

In this article, I try to explain the Out variables in C# step by step with some examples. I hope you understand how to use the out variable before and after C# 7.

Leave a Reply

Your email address will not be published. Required fields are marked *