You can declare out variables inline as arguments to the method they are used
if (int.TryParse(input, out int result))Console.WriteLine(result);elseConsole.WriteLine("Could not parse input");
- You declare the out variable where you use it, not on another line above.
- By declaring the out variable where it's used in a method call, you can't accidentally use it before it is assigned.
- 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: