Out Variables in C#

Out Variables in C# 7 with examples

In this article, I am going to discuss the improvement of 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. As part of this article, we are going to discuss the following pointers.

  1. How to work with the Out Parameter Before C# 7?
  2. Working with Out Variable in C# 7.
  3. What is the Scope of Out Parameter?
  4. Can we declare the out variable with var data type from C# 7?
  5. How to Ignore an out parameter in C#?
  6. Out Parameter Using TryParse
How to work with the Out Parameter Before C# 7?

In C#, we generally use the out parameter to pass a method argument’s reference. If you want to use an out parameter, then you need to explicitly specify the out keyword in both the calling method and method definition. Before C# 7, 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 that variable to the method using the out keyword.

Note: The Out Parameter never carries value into the method definition. So, it is not required to initialize the out parameter while declaring.

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

Note: As the Out Parameter never carries the value into the method definition, so, it is mandatory to initialize the out variables within the method definition otherwise you will get a compile-time error. Again you cannot use the “var” data type to declare these variables. Now, the question id it is not required to initialize the out variables then why should we split their usage into two parts. Well, this will overcome with C# 7,

Working with Out Variable in C# 7.

With the introduction of C# 7, now it is possible to 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 our previous program.

What is the Scope of Out Parameter?

As we declared the out parameter directly within the method so we need to understand the scope out parameter. In the above program, the out variables are in the scope of the enclosing block. So the subsequent line can use them.

Can we declare the out variable with var data type from C# 7?

Yes, you can. As the out variables are declared directly as arguments to the out parameters, so, the compiler can easily tell what their data 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";
    }
}
How to Ignore an out parameter in C#?

If you want to ignore an out parameter then you need to use a wildcard called underscore (‘_’) as the name of the parameter. 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 the 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();
    }
}

Run the application and it should give the same output as the previous example. In the above program, we are declaring the out variable within the method and it is being accessed from outside also. If an exception 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. Here, in this article, I try to explain the improvement of Out variables in C# step by step with some examples. 

Leave a Reply

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