Why ArgumentNullException? Why not System.NullReferenceException?
Asked Answered
E

4

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?

Eyra answered 9/8, 2013 at 11:36 Comment(6)
possible duplicate of ArgumentNullException or NullReferenceException from extension method?Companion
@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"...Eyra
blog.mischel.com/2013/05/16/…Mechanical
@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.Companion
@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.Eyra
@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.). :)Companion
H
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.

Headspring answered 9/8, 2013 at 11:38 Comment(3)
What do you mean by "dereferencing the _tables variable"?Eyra
It means getting the actual value of the reference - dereferencing (since a reference is just a pointer to an address).Cai
Clear every concept mention in this answer by experts..! extension method I found most intersting. Thank u all for knowlegeful replies.Eyra
F
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.

Futility answered 9/8, 2013 at 11:38 Comment(0)
J
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).

Jefferey answered 9/8, 2013 at 11:38 Comment(0)
C
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.

Croesus answered 9/8, 2013 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.