Saturday, October 3, 2020

Whats New in C# 7.0 - Part 1


out variables - C# 7.0

You can declare out variables inline as arguments to the method they are used 

if (int.TryParse(input, out int result))
    Console.WriteLine(result);
else
    Console.WriteLine("Could not parse input");


  1. You declare the out variable where you use it, not on another line above.
  2. By declaring the out variable where it's used in a method call, you can't accidentally use  it before it is assigned.

  

  1. You may want to specify the type of the out variable for clarity, as shown above. However, the language does support using an implicitly typed local variable: