What are the improvements in Out Parameter in C# 7.0?


We can declare out values inline as arguments to the method where they're used.

The existing out parameters has been improved in this version. Now we can declare out variables in the argument list of a method call, rather than writing a separate declaration statement.

Advantages

  • The code is easier to read.

  • No need to assign an initial value.

Existing Syntax

Example

class Program{
   public static void AddMultiplyValues(int a, int b, out int c, out int d){
      c = a + b;
      d = a * b;
   }
   public static void Main(){
      int c;
      int d;
      AddMultiplyValues(5, 10, out c, out d);
      System.Console.WriteLine(c);
      System.Console.WriteLine(d);
      Console.ReadLine();
   }
}

Output

15
50

New Syntax

Example

class Program{
   public static void AddMultiplyValues(int a, int b, out int c, out int d){
      c = a + b;
      d = a * b;
   }
   public static void Main(){
      AddMultiplyValues(5, 10, out int c, out int d);
      System.Console.WriteLine(c);
      System.Console.WriteLine(d);
      Console.ReadLine();
   }
}

Output

15
50

Updated on: 19-Aug-2020

122 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements