Search code examples
c#linqc#-8.0nullable-reference-types

Using Linq's Where/Select to filter out null and convert the type to non-nullable cannot be made into an extension method


Suppose I have

List<MyObject?> list = ...;

I want to turn it into List<MyObject>, but I have not been able to drop the nullable reference.

Below is an MCVE. In my project I have nullable reference warnings turned to errors, so the commented out line below will not compile.

If I do .Where(e => e != null).Select(e => e!) then it will be fine in the latest .NET Core 3.1.100, however I cannot extract this into an extension method.

I tried adding this extension method

    public static IEnumerable<T> NotNull<T>(this IEnumerable<T> enumerable)
    {
        return enumerable.Where(e => e != null).Select(e => e!);
    }

However it will not convert IEnumerable<MyObject?> to IEnumerable<MyObject> and I am unsure why. This leads me to an error like:

[CS8619] Nullability of reference types in value of type 'List' doesn't match target type 'List'.

Is there a way I can make the NotNull function above work somehow?


Solution

  • You have to update your extension method to the following

    public static IEnumerable<T> NotNull<T>(this IEnumerable<T?> enumerable) where T : class
    {
        return enumerable.Where(e => e != null).Select(e => e!);
    }
    

    The point here is that you are converting the IEnumerable of nullable references to not nullable ones, therefore you'll have to use IEnumerable<T?>. where T : class generic constraint is needed to help the compiler distinguish between nullable reference type and Nullable<T> struct, as you can read here

    Because of this issue between the concrete representations of nullable reference types and nullable value types, any use of T? must also require you to constrain the T to be either class or struct.

    After that the following lines will be compiled without any warnings

    var list = new List<MyObject?>();
    IEnumerable<MyObject> notNull = list.NotNull();