Nullable reference types with generic return type
You were very close. Just write your method like this: [return: MaybeNull] public T Get<T>(string key) { var wrapper = cacheService.Get(key); return wrapper.HasValue ? Deserialize<T>(wrapper) : default!; } You have to use the default! to get rid of the warning. But you can tell the compiler with [return: MaybeNull] that it should check for null … Read more