C# 中 returns 多个值的函数的方法签名

Method signature of the function which returns multiple value in C#

我有下面的方法调用 returns 多个值,而且该函数是异步类型的。

var (averageSalary, numberOfEmployee) = await SomeCalculation(0L, 10, 0L == 10L);

现在我需要编写函数的方法签名和return类型。下面是代码:

private static void Main(string[] args)
    {
        var (averageSalary, numberOfEmployee) = await SomeCalculation(0L, 10, 0L == 10L);
    }

    public static async Task<(long averageSalary, long numberOfEmployee)> SomeCalculation(long num1, long num2, bool b)
    {
        long averageSalary = num1;
        long numberOfEmployee = num2;

        return (averageSalary, numberOfEmployee);
    }

但问题是在main方法中调用该方法时,出现以下错误。

The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

谁能给我写正确方法签名的建议。

您的代码应如下所示:

public static async Task<(long averageSalary, long numberOfEmployee)> SomeCalculation(long num1, long num2, bool b)
{
    long averageSalary = num1;
    long numberOfEmployee = num2;

    return (averageSalary, numberOfEmployee);
}

static async Task Main(string[] args)
{
    var (averageSalary, numberOfEmployee) = await SomeCalculation(0L, 10, 0L == 10L);
}

您可能需要将 Visual Studio 中的 C# 版本重置为最新版本(7.1 或更高版本)。单击项目,单击鼠标右键,单击 "Build",然后单击 "Advanced" 按钮。解释 here.