Tech Off Post

Single Post Permalink

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

    That doesnt however explain  to me why (null + null) will result in string.Empty (see IL below)

      IL_0000:  ldstr      ""
      IL_0005:  callvirt   instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()
      IL_000a:  call       void [mscorlib]System.Console::WriteLine(object)
      IL_000f:  call       string [mscorlib]System.Console::ReadLine()

    I understand that overloaded string + operator is being called and nulls converted to "". But why string is chosen in the first place? IMO it should not be possible to compile this code at all...

    Regarding my seconds example, C# compiler is being confused by the use of constructors and does no type checking, so that the following would still compile :

    public string h = 56;
    public DateTime k = 0;
    public int i = "dfsf";
    public Class1() : this(0)

    public Class1(int i) : this()