title: return default value or null for Task<T>
body: <p>I have a wrapper around Cache library and have the following method to retrieve the cached valued based on Key:</p>
  
  <pre><code>public async Task&lt;T&gt; GetAsync&lt;T&gt;(string key)
  {
      var serializedObject = await _cache.GetStringAsync(key);
  
      return JsonConvert.DeserializeObject&lt;T&gt;(serializedObject);
  }
  </code></pre>
  
  <p>The problem is there is nothing in cache, I receive an error from the DeserializeObject method. I'm stuck on what how to return either null or the default value of T when nothing is stored cache.</p>
  
  <p>I tried:</p>
  
  <pre><code>public async Task&lt;T&gt; GetAsync&lt;T&gt;(string key)
  {
      var serializedObject = await _cache.GetStringAsync(key);
  
      if (serializedObject == null) return Task.CompletedTask;
  
      return JsonConvert.DeserializeObject&lt;T&gt;(serializedObject);
  }
  </code></pre>
  
  <p>But Task.CompletedTask cannot be converted to <code>Task&lt;T&gt;</code></p>
  
  <p>I tried:</p>
  
  <pre><code>public async Task&lt;T&gt; GetAsync&lt;T&gt;(string key)
      {
          var serializedObject = await _cache.GetStringAsync(key);
  
          if (serializedObject == null) return Task.FromResult&lt;T&gt;(null);
  
          return JsonConvert.DeserializeObject&lt;T&gt;(serializedObject);
      }
  </code></pre>
  
  <p>But null is not a valid parameter for T. </p>
  
  <p>How do I get GetAsync to return either null or the default value of T? </p>
  
  <p><strong>Update</strong></p>
  
  <p>I got the following to work:</p>
  
  <pre><code>public async Task&lt;T&gt; GetAsync&lt;T&gt;(string key)
  {
      var serializedObject = await _cache.GetStringAsync(key);
  
      return serializedObject == null ? default(T) : JsonConvert.DeserializeObject&lt;T&gt;(serializedObject);
  }
  </code></pre>