Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generically checking for null that won't box nullables on a non-constrained type.

Tags:

c#

generics

Let's say I have the following method:

public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable)
{
    if (enumerable == null) throw new ArgumentNullException("enumerable");
    int count = 0;
    foreach (var x in enumerable)
    {
        if (x != null) count++; 
    }
    return count;
}

And I have these 3 arrays::

var ints = Enumerable.Range(0,10).ToArray();
var nullableInts = Array.ConvertAll(ints,x=>x as int?);
var strings = Array.ConvertAll(ints,x => x.ToString());

I wrote a little function to do a loop and time it for a million iterations. Applying it to ints and strings, it finishes in about 100 ms on my machine. For nullableInts, it takes 2.5 seconds. As I understand the check for null on int doesn't make sense, so the compiler has a different template for non-nullable struct types, that removes null checks. But Nullable<T> does not have a template that converts the null check to x.HasValue. If I have an unconstrained function, how can I do a null check that will perform well? I can't use EqualityComparer<T>, as null might not be a member of T as there is no constraint.

Also it's impossible to have overloads that differ by constraint, so I can't, say, have one for structs, one for Nullable<T>, and one for classes.

The caller of the method is non-constrained. This is just an example (not the actual method); the method calling is non-constrained. I need to do some work against non-null members, and it's a generic method. I suppose I could write a version that doesn't do the check vs one that does (and consequently has a different signature), but it's seems very ugly and unneeded.

Also, the extension method .Count inexplicably performs horribly for NullableInts and strings, (equally bad), so it really isn't the right approach. This might be the delegate invocation, but I doubt it. Using the UnboxT style method of Check<T>.IfNull performs a lot better. Okay, really weird switching the body of the count to this performs great:

    public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable)
    {
        return enumerable.Count(Check<T>.IfNull.Invoke);
    }

Why?

like image 206
Michael B Avatar asked Jan 01 '26 07:01

Michael B


1 Answers

You can constrain generic type parameters to reference types or values types:

public static int CountNonNull<T>(this IEnumerable<T> source)
    where T : class
{
    return source.Count(x => x != null);
}

public static int CountNonNull<T>(this IEnumerable<Nullable<T>> source)
    where T : struct
{
    return source.Count(x => x.HasValue);
}

You don't need an overload for non-nullable structs, because they can't be null anyway.

like image 156
dtb Avatar answered Jan 03 '26 22:01

dtb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!