C# 7.0 Feature : Improvements Out Variables




The out keyword was used to pass a method argument's reference. Before a variable is passed as an out argument, it must be declared.

To use an out parameter, both the method definition and the calling method must explicitly use the out keyword.

Before

class Program   
{
  static void Main(string[] args)   
  {
   // declare the out variable before using.   
      string EmployeeName, EmployeeTitle;   
      long JoiningYear;

  // calling the method.
     GetEmployee(out EmployeeName, out EmployeeTitle, out JoiningYear);   

     Console.WriteLine("EmployeeName: {0}, EmployeeTitle: {1}, JoiningYear: {2}",   

     EmployeeName, EmployeeTitle, JoiningYear);   

    Console.ReadKey();   
 }   

static void GetEmployee(out string EmployeeName, out string EmployeeTitle, out long JoiningYear)  
 {   
    EmployeeName = "kamal kumar das";   
    EmployeeTitle = "Technical Architect";   
   JoiningYear = 2010;   
 }    
}  

In C#7

You can define a method's out parameters directly in the method. No need to declare before passing to calling method.

class Program   
 
static void Main(string[] args)   
{
  //Now no need to declare the out variable before using.   
   
// calling the method.
  GetEmployee(out string EmployeeName, out string EmployeeTitle, out long JoiningYear);
   
  Console.WriteLine("EmployeeName: {0}, EmployeeTitle: {1}, JoiningYear: {2}",   
  EmployeeName, EmployeeTitle, JoiningYear);   

  Console.ReadKey();   
 
static void GetEmployee(out string EmployeeName, out string EmployeeTitle, out long JoiningYear)  
{   
  EmployeeName = "kamal kumar das";   

  EmployeeTitle = "Technical Architect";   

  JoiningYear = 2010;   
 
}   



Review: whats new in C#7.0
C# 7.0 Feature : Improvements Out Variables C# 7.0 Feature : Improvements Out Variables Reviewed by kamal kumar das on January 09, 2017 Rating: 5

No comments:

ads 728x90 B
Powered by Blogger.