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?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74
  • 1
    possible duplicate of [ArgumentNullException or NullReferenceException from extension method?](http://stackoverflow.com/questions/463302/argumentnullexception-or-nullreferenceexception-from-extension-method) – Sam Harwell Aug 09 '13 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"... – Ankush Madankar Aug 09 '13 at 11:49
  • 1
    http://blog.mischel.com/2013/05/16/null-parameters-in-extension-methods/ – Jim Mischel Aug 09 '13 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. – Sam Harwell Aug 09 '13 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. – Ankush Madankar Aug 09 '13 at 12:49
  • @AnkushMadankar It's ok to ask a duplicate question, *especially* if you already searched and couldn't find it (different name, didn't know what you were looking for, etc., etc.). :) – Sam Harwell Aug 09 '13 at 13:06

4 Answers4

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
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.

It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
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).

Plymouth223
  • 1,905
  • 1
  • 12
  • 22
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.

It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103