Possible Duplicate:
C#: Why is adding null to a string legal?
Here is the valid C# code:
var samsung= "xyz" + null + null + null + null + "890"; // xyz890
And this is the invalid C# code:
var iphone= null.ToString(); // compiler error
Why and how is the first statement valid and second is invalid ?
The
+operator is used to concatenate strings, and sincenullis an instance of the string class it can be used as an argument to the operator."xyz" + nullends up returning the string “xyz”, so this process is just repeated until you actually add"890".While null can be used as a string object for an argument to a method, you cannot actually call methods on it because there’s nothing to process the method call.
Think of methods as a way for an object to process an external request and things make a bit more sense. You can send a request to an object with
nullas an argument for what to process, but you can’t actually asknullto process something.