How to Handle Nulls Passed to Extension Methods

J

Jehu Galeahsa

Hello:

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

Thanks,
Travis Parks
 
T

Tom Shelton

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.
 
P

Peter Duniho

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
 
P

Patrice

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...
 
A

Arne Vajhøj

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
 
J

Jehu Galeahsa

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?
 
P

Peter Duniho

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top