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?

@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 HarwellAug 9 '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 MadankarAug 9 '13 at 12:49

4 Answers
4

... so _tablesis 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.

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.

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).