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

C# 7.0 introduced significant improvements to out parameters that make code more concise and readable. The major enhancement is the ability to declare out variables inline as arguments to the method where they're used, eliminating the need for separate variable declarations.

Syntax

Prior to C# 7.0, out variables had to be declared before use −

int result;
if (int.TryParse("123", out result)) {
   // use result
}

C# 7.0 allows inline declaration −

if (int.TryParse("123", out int result)) {
   // use result immediately
}

Key Improvements

  • Inline Declaration: Variables can be declared directly in the method call.

  • Type Inference: You can use var for automatic type detection.

  • Reduced Code: No need for separate declaration statements.

  • Better Scope: Variables have more limited scope, reducing potential errors.

Out Parameter Evolution in C# 7.0 Before C# 7.0 int result; TryParse("123", out result); Separate declaration required C# 7.0 and Later TryParse("123", out int result); Inline declaration More concise code

Using Traditional Out Parameter Syntax

Example

using System;

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);
      Console.WriteLine("Sum: " + c);
      Console.WriteLine("Product: " + d);
   }
}

The output of the above code is −

Sum: 15
Product: 50

Using C# 7.0 Inline Out Parameter Declaration

Example

using System;

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);
      Console.WriteLine("Sum: " + c);
      Console.WriteLine("Product: " + d);
   }
}

The output of the above code is −

Sum: 15
Product: 50

Using var with Out Parameters

Example

using System;

class Program {
   public static void Main() {
      string input = "42";
      
      if (int.TryParse(input, out var number)) {
         Console.WriteLine("Parsed number: " + number);
         Console.WriteLine("Number type: " + number.GetType().Name);
      } else {
         Console.WriteLine("Failed to parse: " + input);
      }
      
      if (DateTime.TryParse("2023-12-25", out var date)) {
         Console.WriteLine("Parsed date: " + date.ToShortDateString());
      }
   }
}

The output of the above code is −

Parsed number: 42
Number type: Int32
Parsed date: 12/25/2023

Comparison

Feature Before C# 7.0 C# 7.0 and Later
Declaration Separate declaration required Inline declaration allowed
Type Inference Explicit type required Can use var keyword
Code Lines More lines of code Fewer lines, more concise
Variable Scope Broader scope Limited to necessary scope

Conclusion

C# 7.0's improved out parameters make code more readable and concise by allowing inline variable declaration. This enhancement reduces boilerplate code and enables better variable scoping, making the feature particularly useful with methods like TryParse and custom methods that return multiple values.

Updated on: 2026-03-17T07:04:36+05:30

257 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements