
上QQ阅读APP看书,第一时间看更新
Out variables
If you want to pass parameters to a method for modification, then you always need to declare them first. This is no longer necessary, and you can simply declare the variables at the point you pass them in. You can also declare a variable to be discarded, using an underscore. This is particularly useful if you don't want to use the returned value, for example, in some of the try parse methods of the native framework data types.
Here, we parse a date without declaring the dt variable first:
DateTime.TryParse("2017-08-09", out var dt);
In this example, we test for an integer, but we don't care what it is:
var isInt = int.TryParse("w00t", out _);