Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Use Async in Main function

As per this post, the top voted answer suggested that we can use async directly in main. Or I misunderstood it?

Can't specify the 'async' modifier on the 'Main' method of a console app

My main class:

public class Program
{
    public static async Task Main(string[] args)
    {
        ApproveEvents ap = new ApproveEvents();
        List<MyModel> result = new List<MyModel>();
        result = await ap.ApproveAsync();
        if (result.count > 0)
        {
            //do something here
        }

    }
}

And,

public class ApproveEvents
{
    public async Task<List<MyModel>> ApproveAsync()
    {
        //blah blah
    }
}

Visual Studio 2017 is complaining about no Main method for an entry point.

How should I resolve this?

like image 805
xzk Avatar asked Sep 11 '26 08:09

xzk


1 Answers

async Task Main is available in C# 7.1. You can change it in build properties (the default is the latest major version, which is 7.0)

like image 89
AlexK Avatar answered Sep 13 '26 20:09

AlexK