Out parameters are the most common feature we use in our regular development in C#. Out parameters are the parameters which will be sent by the calling method and expects this variable will be assigned with some appropriate value.  

Public int Calc(int a, int b) 
{ 
Int result; 
DoSum(a, b, out result); 
Return result; 
} 
 
Public void DoSum(int a, int b, out int result) 
{ 
     Result = a + b; 
}

Let’s understand the basic use of Out parameter from the above code snippet before going to learn about new enhanced out variable feature in C# 7.0.

Here, the method Calc() sending two integer values and expecting a sum of these values as a result. In this method, we are calling another method DoSum() which accepts two integer variables and one out parameter. Here out is a keyword, when compiler compiles this code, it won’t expect this result variable to be initialized before sending it via. DoSum method and also compiler raises a run-time exception is this result variable is not assigned with any value in the DoSum method. So, its guarantee that some value will be assigned by DoSum method before returning.

We also having TryParse Pattern methods in our Framework which will return true/false based on conversion and assigns a value if success. For example, following statement will typecast from string to integer. If this string contains integer value, it will return true and this string value will be assigned to this integer variable, in case of failed to convert this string to int, it will return false and won’t assign any value to this int variable.

int a; 
 
If(int.TryParse(str, out a)) 
{}

Enough to know about out parameters, now will try to understand the existing problems and new solutions for these problems. 

Problem-1: Whenever I want to send a variable as out parameter, need to declare this new variable without assigned. In the above example, we declared a variable and sent this variable as out parameter to the TryParse method. 

Solution: Now from C# 7.0, we can directly declare and send this variable as an Out parameter there itself. For the above example, now we can write as following in a single statement. 

 If(int.TryParse(str, out int a)) 
{}

This way we avoid unnecessary pre declaring the variable in advance. This way of declaring a variable and using as out parameter is called as Out Variables.

Problem-2: There will be some situations where we don’t want to pass all the out variables to a given method. But we need to unnecessary declare all the expected out parameters, which leads to unnecessary memory usage and warnings by the IDE as unused variables.

Solution: Let us understand below statement.

GetColor(string hexClolor, out int r, out int g, out int b, out int a) 
{---}

In the above statement, we have a method called GetColor and this method accepts hex representation of color code (ex. #FF0000) and decode this code and returns its Red, Green, Blue, Alpha values via. Out parameters.

Now, as GetColor method is a generic method and can use across different applications. But my requirement is to use this method and I am not interested in alpha value but I am interested in getting Red, Green, Blue values of given hexadecimal color code. But unfortunately, previously we need to even predeclare an alpha variable (a) as this is not needed for us and leads to unnecessary memory allocations and warnings from IDE saying “unused variable”. Below is the calling statement.

GetColor(string hexClolor, out int r, out int g, out int b, out int a);

To solve this problem of unnecessary memory allocations and warnings from IDE saying “unused variable”, Discard feature is introduced from C# 7.0Here we can keep (_) underscore symbol wherever you are not interested in any out parameter as shown below 

GetColor(string hexClolor, out int r, out int g, out int b, out _);

Now the runtime won’t allocate any memory for the alpha variable (a) and no warnings from IDE about unused variable and also neat code without unnecessary variable declarations.

The same Discards will be useful while we are dealing with Tuples (can get more explore about Tuples here) as below.  

(int r, int g, int b, _) = GetColor(" #FF0000");

Hope, you understand both Out Variable and Discard features introduced from C# 7.0.

Please Leave a comment below. I would love to hear your thoughts on this post!

Happy Coding 🙂