Tech Off Post

Single Post Permalink

View Thread: null + null = ?
  • User profile image

    Londoner wrote:
    What would you expect the result of the following code to be?


    static void Main( string[] args )


          Console.WriteLine( ( null + null ).GetType() );



    Now compile it and see for yourself. Magic...

    hey londoner - sorry for the very late reply Smiley...i got an answer from one of the c# compiler guys on this:

    The compiler is applying "overload resolution" on the + operator to pick the best +. The only predefined + operator for which null can be used as an argument is the string concatenation operator.
    Note that this will change in Whidbey. With support for nullable types, there are now other predefined + operators that work, like +(int?, int?). This code will then generate an ambiguity error.