c#7.0 out作用域相关 新增
1.C# 7 that allows you to declare the variable at the point where you want to use it as an argument.
c#7允许在任何参数位置声明out参数
2.Since the out variables are declared directly as arguments to out parameters, the compiler can usually tell what their type should be (unless there are conflicting overloads), so it is fine to use var instead of a type to declare them
编译器会检测出变量类型,在形参处可使用out var推断类型
3.The in-line declared Out parameter can be accessed in the same block. Its scope is in the method where it calls
当行声明的out变量的声明周期是调用它的函数范围。
示例:
static void Main(string[] args)
{
WriteLine(“Please enter radious for circle”);
double radious = Convert.ToDouble(ReadLine());
double circumference = CalculateCircle(radious, out double area); //declare in-line
WriteLine("Circle′scircumferenceiscircumference");WriteLine("Circle's circumference is {circumference}");
WriteLine("Circle′scircumferenceiscircumference");WriteLine(“Circle’s Area is {area}”); //still valid
ReadKey();
}
static double CalculateCircle(double radious, out double area)
{
area = Math.PI * Math.Pow(radious, 2);
double circumference = 2 * Math.PI * radious;
return circumference;
}
参考链接:https://devblogs.microsoft.com/dotnet/whats-new-in-csharp-7-0/
https://www.c-sharpcorner.com/article/out-parameter-in-c-sharp-7/