8

See line of code below:

DataTable [] _tables = null;

// Throws System.NullReferenceException
_tables.GetType();

// Throws System.ArgumentNullException
_tables.Count();

In this lines of code I have _tables reference and trying to access its system define functions GetType() and Count(), both throw exception but why .Count() throws System.ArgumentNullException, since we have same value for reference which is null?

6
  • 1
    possible duplicate of ArgumentNullException or NullReferenceException from extension method? Commented Aug 9, 2013 at 11:42
  • @280Z28 I think in that que, Patrik Hägne ask for "best exception type to throw when an extension method called on a null instance"... Commented Aug 9, 2013 at 11:49
  • 1
    blog.mischel.com/2013/05/16/… Commented Aug 9, 2013 at 12:07
  • @AnkushMadankar The accepted answer in that question explains not only the accepted practice that results in your call getting an ArgumentNullException, but the reason why that practice was originally chosen. Commented Aug 9, 2013 at 12:36
  • @280Z28 If I know practice used in ArgumentNullException , I never ask this question, rather search for practice and got answer for same, but while asking I wasn’t aware about that practice.. I already search for similar question but wasn’t found any one hence put this question. Commented Aug 9, 2013 at 12:49

4 Answers 4

20

Count() is an extension method on IEnumerable<T>, declared in System.Linq.Enumerable - so you're actually calling:

Enumerable.Count(_tables);

... so _tables is a method argument, and it makes sense for the exception to tell you that. You're not actually dereferencing the _tables variable when you call Count(), whereas you are when you call GetType.

3
  • What do you mean by "dereferencing the _tables variable"? Commented Aug 9, 2013 at 11:56
  • 2
    It means getting the actual value of the reference - dereferencing (since a reference is just a pointer to an address).
    – Charleh
    Commented Aug 9, 2013 at 12:04
  • Clear every concept mention in this answer by experts..! extension method I found most intersting. Thank u all for knowlegeful replies. Commented Aug 9, 2013 at 12:38
7

Because Count here is a call to an extension-method with _tables as the argument - it is actually:

System.Linq.Enumerable.Count(_tables);

If you don't want to use the extension method: use _tables.Length.

4

Because it's an extension method, and not an instance method.

As it's compiled to Enumerable.Count(_tables), it doesn't apply to NullReferenceException, so it just throws an ArgumentNullException instead. However, GetType is an instance method, so you are trying to call a method on null, which... um, doesn't work.

4

Count() is an extension method (it should therefore throw an ArgumentNullException if a passed in value is null and null is illegal), not a method on the instance of the object i.e. Count is defined as public static int Count<T>(this IEnumerable<T> source).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.