Nick Strupat
Nick Strupat

Reputation: 5063

What should I throw when the `this` parameter is null in extension methods?

What should I throw when the this parameter is null in extension methods?

If I should throw ArgumentNullException, what should the name of the parameter be?

Upvotes: 1

Views: 82

Answers (3)

Daniel Powell
Daniel Powell

Reputation: 8303

In the end extension methods are kind of just fancy static methods so I'd take the same approach you would with them

Upvotes: 1

Andrew Barber
Andrew Barber

Reputation: 40160

I think you could consider doing nothing about it, because what will happen is a NullReferenceException will be caused in the extension body for that parameter, which is actually exactly what the semantics appear like on the usage of the extension method.

Upvotes: 2

Fyodor Soikin
Fyodor Soikin

Reputation: 80840

Yes, throw an ArgumentNullException, and provide whatever the name of your parameter really is.

Upvotes: 4

Related Questions