From: Jehu Galeahsa on
Hello:

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

Thanks,
Travis Parks
From: Tom Shelton on
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.

--
Tom Shelton


From: Peter Duniho on
Jehu Galeahsa wrote:
> 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
From: Patrice on
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...

--
Patrice


From: Arne Vajhøj on
On 10-05-2010 23:25, Jehu Galeahsa wrote:
> 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



 |  Next  |  Last
Pages: 1 2
Prev: Interface as a field
Next: Newsgroup Server Software