0

I have the below method calling which returns multiple values and also the function is asynchronous type.

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

Now I need to write the method signature and return type of the function. Below is the code:

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);
    }

But the problem is when the method is calling in the main method, the below error is showing.

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'.

Can anyone give me the suggestion to write the correct method signature.

mnu-nasir
  • 1,642
  • 5
  • 30
  • 62

1 Answers1

1

Here is how your code should look like:

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);
}

You may need to reset your C# version in Visual Studio to a recent version (7.1 or later). Click the project, right mouse button, click on the "Build" and then on the "Advanced" button. It is explained here.

Daan
  • 2,478
  • 3
  • 36
  • 76