title: How to cast a typed value to generic type T?
body: <p>I'm trying to use Azure Redis Cache in .NET Core application. I have written a CacheHelper generic method which implements generic interface like below.</p>
  
  <p>However this issue is more related to Generics.</p>
  
  <p>I'm getting error while casting cache value to generic type. Is this the correct way to do? or Am i missing something? Please see the code below.</p>
  
  <pre><code>public class CacheHelper&lt;T&gt; : ICacheHelper&lt;T&gt;
  {
      private ICacheProvider _cacheProvider;
  
      public CacheHelper(ICacheProvider cacheProvider)
      {
          _cacheProvider = cacheProvider;
      }
  
      public async Task&lt;CacheResponse&lt;T&gt;&gt; GetCacheValueAsync(string key)
      {
          RedisValue value = await _cacheProvider.GetCacheDatabase().StringGetAsync(key);
          if (typeof(T).Equals(typeof(string)))
          {
              return value.HasValue
              ? new CacheResponse&lt;T&gt;(value) //I'm getting error here cannot cast type RedisValue to type T
              : CacheResponse&lt;T&gt;.NoValue();
          }
          else
          {
              T obj = JsonConvert.DeserializeObject&lt;T&gt;(value);
              return value.HasValue
             ? new CacheResponse&lt;T&gt;(obj)
             : CacheResponse&lt;T&gt;.NoValue();
          }
      }
  }
  </code></pre>