views:

1080

answers:

5

What would you consider to be the best exception type to throw when an extension method is called on a null instance (where the extension method does not allow it)? Since extension methods are nothing but static methods you could argue that it should be ArgumentNullException, but on the other hand they're used like instance methods so it might be more natural to use the NullReferenceException. Let's take the following example:

public static string ToInvariantString(this IFormattable value, string format)
{
    return value.ToString(format, CultureInfo.InvariantCulture);
}

This way a NullReferenceException will be thrown if the value parameter is null.

The other example would be:

public static string ToInvariantString(this IFormattable value, string format)
{
    if (value == null) throw new ArgumentNullException("value");
    return value.ToString(format, CultureInfo.InvariantCulture);
}

EDIT: In some of the answers you have pointed out that an extension methods can be called like a static method and in those cases a null reference exception would be wrong, which is a great point, and actually one of my concerns, not sure why I forgot to mention that in the question in the first place.

Someone also pointed out that it's wrong to throw a NullReferenceException, and yes, it is. That's why I don't throw it, I just let it happen (let the CLR throw it) by not guarding the method.

I think I favor the ArgumentNullException (that's what I've use so far) but I still think there is at least room to argue for an against the NullReferenceException since it seems more natural in most places where the method is going to be used.

+1  A: 

From the user's standpoint, the method looks and acts like an instance method, so if I were them, I would expect to see a NullReferenceException.

That said, I would suggest throwing either one or the other explicitly in the code, instead of just "happening" to throw one as in your first example.

mquander
Yes, I've thought of throwing the NullReferenceException explicitly to, but my feeling is that it should be reserved for the compiler really, but you may very well be right.
Patrik Hägne
From a users standpoint an extension method can be called both by extension or as a static method.
JaredPar
I believe that if the intent of the method is to behave as an instance method, then it should do so as best as possible, which means `NullReferenceException`. For the same reason, I would never write an extension method to which `null` is a valid/properly handled first parameter.
280Z28
+2  A: 

ArgumentNullException. There is no requirement to call extension methods as though they were instance methods. You can call them as if they were normal methods. NullReferenceException would be completely incorrect in that case.

Craig Stuntz
+4  A: 

Since extension methods can be used in C# 2.0, and they can be called just like static methods (you do not HAVE to use them as extension methods), you should use ArgumentNullException.

Just because they look like methods on the type doesn't mean that they are, or are always called like one.

casperOne
+8  A: 

In general, exceptions included, you should treat an extension method as if it were a normal static method. In this case you should throw an ArgumentNullException.

Throwing a NullReferenceException here is a bad idea for a few reasons

  • A null reference did not actually occur so seeing one is counterintuitive
  • Throwing a NullReferenceException and causing a NullReferenceException to occur produce discernably different exceptions (One way to see the difference is the error code). This is true of many exceptions that are thrown by the CLR.

See a post I did on this subject: http://blogs.msdn.com/jaredpar/archive/2008/10/22/when-can-you-catch-a-stackoverflowexception.aspx

  • It's prefectly legal to call an extension method just as if it were a regular method. In that case I would certainly not except a NullReferenceException, but instead an ArgumentNullException.
JaredPar
Great comments and as I mention in my edit of the post this is the reason i don't explicitly throw the NullReferenceException, I still let the CLR throw it.
Patrik Hägne
+6  A: 

Aside from all the other answers (which are good) I think it's worth looking at what Microsoft does for the sake of consistency... and the extension methods in Enumerable all throw ArgumentNullException as far as I can see.

Jon Skeet
Great idea, I had that myself, didn't do it, not sure why! ;-)
Patrik Hägne
This is a useful technique which I use myself very often. However, one should always bear in mind that just because the FCL uses a particular method to acheive something, it doesn't mean that method is optimal or correct. I recommend thinking of FCL methods as suggestions for how to achieve something, but the merit of a given method ought to be compared to alternatives before judging whether it the best choice.
AdamRalph