Automatic .ToString()?

前端 未结 2 468
耶瑟儿~
耶瑟儿~ 2020-12-21 09:30

I have a method like this: void m1(string str) and have a class like this:

public class MyClass
{
    public bool b1 { set; get; }

    //and ot         


        
2条回答
  •  眼角桃花
    2020-12-21 09:49

    The compiler turns "abcdef" + c1 into a string.Concat(object,object) call. This in turn will call .ToString() on each of the arguments and concatenate them. Here's the code from reflector:

    public static string Concat(object arg0, object arg1)
    {
       if(arg0 == null) arg0 = Empty;
       if(arg1 == null) arg1 = Empty;
    
       return (arg0.ToString() + arg1.ToString());
    }
    

    Note that the last line involves a call to string.Concat(string, string) where the real concatenation happens.

提交回复
热议问题