Passing a null method to a method will not necessarily throw an exception; as for the implementation of the method (in which case, you will probably see ArgumentNullException ).
An attempt to access the member * of a null ** object is that it always throws a NullReferenceException guaranteed by ***.
So...
May or may not throw an exception
object obj = null; SomeMethod(obj);
Defines an exception
object obj = null; int hashCode = obj.GetHashCode();
In the case of the code in question, the parameter that you pass to Console.WriteLine is actually the result of a compiled call to string.Concat , which allows you to pass null values ββas parameters and essentially ignores them - as SLaks already pointed out .
* Extension methods are another matter; they can be called by "zero" parameters; but since they are an illusion of action as instance methods, this rule does not apply to them. In fact, extension methods are, after all, only static methods. If you call a single value "on" a null , you effectively pass null as a parameter.
** Here I do not include Nullable<T> values ββwith HasValue == false ; although in many cases they are conveniently regarded as null , this is just for syntactic convenience: they are no more null than any other type of value can be null .
*** I am talking about C # here. As SLaks notes in a comment, this is not the rule of the CLI itself. But all calls to the instance method in C # are compiled into a callvirt statement in IL, which throws an exception if the instance is null .
Dan Tao Jul 22 '10 at 19:20 2010-07-22 19:20
source share