Shall I always need to throw ArgumentNullException when an extension method is called on null? (Extension methods in Enumerable throw ArgumentNullException.) I would like to have clarification on this. If the answer is both “Yes” and “No” please present both the cases.
Shall I always need to throw ArgumentNullException when an extension method is called on
Share
You need to throw it if the argument is
nulland you don’t support that condition. If that isn’t a problem, there’s no need to throw the exception. One might expect in most cases that a null argument for thethisargument would be an unsupportable condition, but by no means is that always the case.The need for throwing this exception (and for null checking) is no different in extension methods than in traditional methods.