I currently have a generic method where I want to do some validation on the parameters before working on them. Specifically, if the instance of the type parameter T
is a reference type, I want to check to see if it's null
and throw an ArgumentNullException
if it's null.
Something along the lines of:
// This can be a method on a generic class, it does not matter.
public void DoSomething<T>(T instance)
{
if (instance == null) throw new ArgumentNullException("instance");
Note, I do not wish to constrain my type parameter using the class
constraint.
I thought I could use Marc Gravell's answer on "How do I compare a generic type to its default value?", and use the EqualityComparer<T>
class like so:
static void DoSomething<T>(T instance)
{
if (EqualityComparer<T>.Default.Equals(instance, null))
throw new ArgumentNullException("instance");
But it gives a very ambiguous error on the call to Equals
:
Member 'object.Equals(object, object)' cannot be accessed with an instance reference; qualify it with a type name instead
How can I check an instance of T
against null
when T
is not constrained on being a value or reference type?
There's a few ways to do this. Often, in the framework (if you look at source code through Reflector), you'll see a cast of the instance of the type parameter to
object
and then checking that againstnull
, like so:And for the most part, this is fine. However, there's a problem.
Consider the five main cases where an unconstrained instance of
T
could be checked against null:Nullable<T>
Nullable<T>
but is notnull
Nullable<T>
but isnull
null
null
In most of these cases, the performance is fine, but in the cases where you are comparing against
Nullable<T>
, there's a severe performance hit, more than an order of magnitude in one case and at least five times as much in the other case.First, let's define the method:
As well as the test harness method:
Something should be said about the fact that it takes ten million iterations to point this out.
There's definitely an argument that it doesn't matter, and normally, I'd agree. However, I found this over the course of iterating over a very large set of data in a tight loop (building decision trees for tens of thousands of items with hundreds of attributes each) and it was a definite factor.
That said, here are the tests against the casting method:
This outputs:
Note in the case of a non-null
Nullable<T>
as well as a nullNullable<T>
; the first is over fifteen times slower than checking against a value type that is notNullable<T>
while the second is at least eight times as slow.The reason for this is boxing. For every instance of
Nullable<T>
that is passed in, when casting toobject
for a comparison, the value type has to be boxed, which means an allocation on the heap, etc.This can be improved upon, however, by compiling code on the fly. A helper class can be defined which will provide the implementation of a call to
IsNull
, assigned on the fly when the type is created, like so:A few things to note:
default(T)
toobject
in order to see if the type can havenull
assigned to it. It's ok to do here, because it's only being called once per type that this is being called for.T
is notnull
, then it's assumednull
cannot be assigned to an instance ofT
. In this case, there's no reason to actually generate a lambda using theExpression
class, as the condition is always false.null
assigned to it, then it's easy enough to create a lambda expression which compares against null and then compile it on-the-fly.Now, running this test:
The output is:
These numbers are much better in the two cases above, and overall better (although negligible) in the others. There's no boxing, and the
Nullable<T>
is copied onto the stack, which is a much faster operation than creating a new object on the heap (which the prior test was doing).One could go further and use Reflection Emit to generate an interface implementation on the fly, but I've found the results to be negligible, if not worse than using a compiled lambda. The code is also more difficult to maintain, as you have to create new builders for the type, as well as possibly an assembly and module.