Suppress warning CS1998: This async method lacks 'await'

前端 未结 14 941
遇见更好的自我
遇见更好的自我 2020-11-28 04:21

I\'ve got an interface with some async functions. Some of the classes that implements the interface does not have anything to await, and some might just throw. It\'s a b

相关标签:
14条回答
  • 2020-11-28 05:22

    It might be occured cs1998 below.

    public async Task<object> Foo()
    {
        return object;
    }
    

    Then you can reform below.

    public async Task<object> Foo()
    {
        var result = await Task.Run(() =>
        {
            return object;
        });
        return result;
    }
    
    0 讨论(0)
  • 2020-11-28 05:22

    Here is some alternatives depending on your method signature.

        public async Task Test1()
        {
            await Task.CompletedTask;
        }
    
        public async Task<object> Test2()
        {
            return await Task.FromResult<object>(null);
        }
    
        public async Task<object> Test3()
        {
            return await Task.FromException<object>(new NotImplementedException());
        }
    
    0 讨论(0)
提交回复
热议问题