Making interface implementations async

前端 未结 3 1137
太阳男子
太阳男子 2020-12-23 02:26

I’m currently trying to make my application using some Async methods. All my IO is done through explicit implementations of an interface and I am a bit confused about how to

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-23 03:22

    An abstract class can be used instead of an interface (in C# 7.3).

    // Like interface
    abstract class IIO
    {
        public virtual async Task DoOperation(string Name)
        {
            throw new NotImplementedException(); // throwing exception
            // return await Task.Run(() => { return ""; }); // or empty do
        }
    }
    
    // Implementation
    class IOImplementation : IIO
    {
        public override async Task DoOperation(string Name)
        {
            return await await Task.Run(() =>
            {
                if(Name == "Spiderman")
                    return "ok";
                return "cancel";
            }); 
        }
    }
    

提交回复
热议问题