If my interface must return Task what is the best way to have a no-operation implementation?

后端 未结 9 1067
-上瘾入骨i
-上瘾入骨i 2020-11-28 17:59

In the code below, due to the interface, the class LazyBar must return a task from its method (and for argument\'s sake can\'t be changed). If LazyBar

9条回答
  •  心在旅途
    2020-11-28 18:37

    If you are using generics, all answer will give us compile error. You can use return default(T);. Sample below to explain further.

    public async Task GetItemAsync(string id)
                {
                    try
                    {
                        var response = await this._container.ReadItemAsync(id, new PartitionKey(id));
                        return response.Resource;
                    }
                    catch (CosmosException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
                    {
    
                        return default(T);
                    }
    
                }
    

提交回复
热议问题