5

The following generic functions using nullable references worked fine until I installed VS community 16.5.0 preview 2.0 (before this I used preview 1.0, I'm using .NET Core 3.0/3.1):

static async Task<C> Test<C>(MyClass a, MyClass b, Func<MyClass, Task<C>> extract) where C : class? =>
    await extract(a) ?? (b is {} ? await extract(b) : null);

static T MinOrDefault<T>(this IEnumerable<T> enumerable) =>
    enumerable.Any() ? enumerable.Min() : default;

But now both give the error CS8603 Possible null reference return. Does anyone know if this behavior is intentional? And is there a nice way to fix this?

A similar question was asked here, but it does not really have a satisfying answer; especially since it does not work well with Tasks as mentioned in a comment.

SWdV
  • 1,715
  • 1
  • 15
  • 36
  • Which line and statement cause an error? VS 2019 16.4.4 doesn't show anything. Did you try this code in stable version? Or maybe make sense just to disable the _treat warnings as errors_ option – Pavel Anikhouski Feb 07 '20 at 19:15
  • @PavelAnikhouski For both the whole line of the body. I only have the preview installed, as I said this warning/error is a new thing (I guess you could call it a regression). I could disable the option but I prefer strict checks. It would be a possible temporary solution though. – SWdV Feb 07 '20 at 21:14
  • Does adding `where T : notnull` to the `MinOrDefault` method solves your problem? – nalka Feb 10 '20 at 16:16
  • @nalka No it doesn't make a difference – SWdV Feb 10 '20 at 17:09

0 Answers0