New changes with the out variables
Currently in C#, we need to first declare a variable before we pass it as an out
parameter to a method. You can use a var
while declaration if you initialize them in the same line, but when you don't want to initialize explicitly, you must declare them, specifying the full type:
// predeclaration of 'out' variable was mandatory int result; // or, var result = 0; string value = "125"; int.TryParse(value, out result); Console.WriteLine("The result is: " + result);
In C# 7.0, the out
variables can be declared right at the point where they are passed as an out
parameter to a method. You can now directly write int.TryParse(value, out int result);
and get the value of the out
parameter, to use it in the scope of the enclosing block:
static void Main(string[] args) { string value = "125"; int.TryParse(value, out int result); Console.WriteLine("Result: " + result); }
You can also use var
instead of a...