How to Handle Nulls Passed to Extension Methods

  • Thread starter Thread starter Jehu Galeahsa
  • Start date Start date
J

Jehu Galeahsa

Hello:

What is the correct/typical way to handle null "this" arguments passed
to an extension method?

Thanks,
Travis Parks
 
It happens that Jehu Galeahsa formulated :
Hello:

What is the correct/typical way to handle null "this" arguments passed
to an extension method?

Thanks,
Travis Parks

Well, for consistancy, I generally throw a null reference exception -
just like any other instance method would do if called on a jull
reference exception.
 
Jehu said:
Hello:

What is the correct/typical way to handle null "this" arguments passed
to an extension method?

It depends on what the method does and whether a null "this" makes any
sense.

IMHO, you should try to avoid a design where calling a method on a null
"this" makes any sense. And following that philosophy, you should throw
a NullReferenceException if the "this" argument is null (I prefer that
over ArgumentNullException for the "this" in an extension method,
especially since it comes for free most of the time :) ).

But I can't rule out some exceptional case where an operation on "null"
makes sense. In that case, you would not throw the exception.

Pete
 
Hello,
What is the correct/typical way to handle null "this" arguments passed
to an extension method?

I would throw the same exception than when you try to call a method using a
null instance as this is the intended usage...
 
What is the correct/typical way to handle null "this" arguments passed
to an extension method?

I would not handle it.

Meaning that I think you should write the code without
any special test for it and let it cause whatever exception
or not it may.

I don't see extension methods as having responsibility for
enforcing any contracts in this regard, because it is
really a utility method.

Arne
 
It happens that Jehu Galeahsa formulated :




Well, for consistancy, I generally throw a null reference exception -
just like any other instance method would do if called on a jull
reference exception.

I might ask... what would Microsoft do?
 
Jehu said:
I might ask... what would Microsoft do?

You don't have to ask. You can look yourself. And they do it both
ways. I can't speak for Microsoft, but looking at the examples in
System.Linq.Enumerable, I would say that in general, if the
NullReferenceException would occur right away, they don't bother
validating the argument.

It looks like the main reason they validate the argument and throw a
specific exception is if the exception would otherwise be delayed,
especially after the extension method has created a new object, or even
after the extension method returns. For example, methods like Concat()
and Cast() simply copy the passed-in reference to a new object that's
returned by the method. An exception wouldn't occur until the caller
actually tries to use that object.

So, I stand by my previous suggestion: in general, a null "this"
argument should result in a NullReferenceException
(System.Linq.Enumerable uses ArgumentNullException when they check
explicitly…of course a NullReferenceException is thrown when they
don't). If the extension method dereferences the "this" argument
immediately, then no explicit check would be needed. But if you're
storing the argument somewhere and it may be dereferenced later, you
should do an explicit check and throw if it's null.

Pete
 
Back
Top