C# 7 Features - Out Variable Initialization

In my previous article, I discussed about the concept of local functions in C# 7.0. In this article, we will see another feature related to the use of out type variables. 
 
Out variables are not new in C# and provide a big advantage of how we can return multiple values from a method. However, in order to pass an out type variable as a method parameter, we have to declare it first and then pass it into the method parameter with out keyword, which is shown below.
  1. static void Main(string[] args)  
  2. {  
  3.   
  4.             int userId;  
  5.             string userName;  
  6.             GetDetails(out userId, out userName);  
  7.   
  8.             Console.ReadKey();  
  9. }  
  10. public static void GetDetails(out Int32 UserId, out string Username)  
  11. {  
  12.             UserId = 12;  
  13.             Username = "Test User";  
  14. }  
However, in C# 7.0, we can declare the variable at the time of passing it as a method parameter, when the method is being called. This means the code given below will work fine in C# 7.0. 
  1. static void Main(string[] args)  
  2. {  
  3.             GetDetails(out int userId, out string userName);  
  4.             Console.ReadKey();  
  5. }  
Also, we can have the out variable being declared as of var type, when passed as method parameter. Hence, the code given below is valid and works fine in C# 7.0, which did not work in earlier versions of C#. 
  1. static void Main(string[ ] args)  
  2.         {  
  3.             GetDetails(out var userId, out var userName);  
  4.             Console.ReadKey();  
  5.         }  
I hope you enjoyed reading it. Happy coding.