调用方法时放置“out type”声明

Dan*_*l B 3 .net c#

我尝试在 .NET 4.5.2 应用程序中使用“out 参数”,但出现编译错误。

问题:我可以在哪个框架中编译它?这叫什么?out 方法调用中的内联变量声明?

可以请您提供一个参考吗?

参考:https: //www.dotnetperls.com/parse

新的 out 语法:我们可以将“out int”关键字直接放在方法调用中。旧版本的 C# 不允许使用此语法。但这可以减少程序的行数。

static void Main()
{
    const string value = "345";
    // We can place the "out int" declaration in the method call.
    if (int.TryParse(value, out int result))
    {
        Console.WriteLine(result + 1);
    }
}
Run Code Online (Sandbox Code Playgroud)

Bra*_*ner 5

这需要Visual Studio 2017 附带的C# 版本 7 。

该功能称为“输出变量”。

在 C# 7.0 中我们添加了变量;能够在变量作为输出参数传递的位置声明变量:

public void PrintCoordinates(Point p) 
{
    p.GetCoordinates(out int x, out int y);
    WriteLine($"({x}, {y})"); }
}
Run Code Online (Sandbox Code Playgroud)

可以在此处找到更多文档。