What is the difference between , and + while concatenating?

前端 未结 3 773
傲寒
傲寒 2021-01-29 15:41

I have been coding with c# for a last couple of months now, but every time I concatenate I always get confused between the difference between the comma , and the pl

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-29 16:27

    The + is "addition operator". For numbers, it adds them. For dates and timespans, it adds them. For strings (texts) - it joins them, becase there's hardly any other sense of "adding" a text to a text.

    Moreover, the + is able to "add"/"join"/"concatenate" a non-text to a text, so "mom" + 5 will actually produce "mom5" text. But this is a hack. What it does in this case, is actually:

    "mom" + (5).ToString()
    

    so it calls "ToString()" on the integer 5, gets a string "5" as a result, and adds/joins two texts together. This way it is able to "add" anything to a text - it just "tostring"s everything and joins the strings afterwards.

    Now, the second thing:

    Commas never work as concatenation. Period.

    Commas are for passing parameters to a function/method. WriteLine is a method, it takes some parameters. When you call a method by writing method( ), you can pass parameters inside ( ), i.e. method( 5 ) or method( "foo" ). If there would be more than one parameter, then they must be separated by , (commas): method( 5, "foo", "bar"), and it does not mean the 5, foo and bar would be concatenated. They will be passed to the method, and the method will do ... something with them.

    It only so happens that the WriteLine function may concatenate things. You can read what it exactly does on MSDN or other docs. In short, it takes so-called "format string" and then some extra parameters, and then glues that extra parameters into the "format string" to build a result. For example:

    format:  Hello {0}!
    param1:  Ted
    code:    WriteLine("Hello {0}!", "Ted");
    result:  Hello Ted!
    
    format:  Hello {0}!
    param1:  12.36
    code:    WriteLine("Hello {0}!", 12.36);
    result:  Hello 12.36!
    
    format:  Hello {0} and {1}!
    param1:  John
    param2:  Bob
    code:    WriteLine("Hello {0} and {1}!", "John", "Bob");
    result:  Hello John and Bob!
    
    code:    WriteLine("I ate {0} {1} {2}!", 5, "bad", "bananas");
    result:  I ate 5 bad bananas!
    

    The {0} {1} etc markers are .. markers. They denote the place where one of the the parameters will be put into. Common name for such marker is "placeholder".

    WriteLine writes the text directly to the console. A more useful similar function is string.Format. It works the same way: takes a format, some params, builds a result and returns it as a string so you can do something with it other than just dumping it to console.

    Check. Consider this, and think how to write it using just "+" without formatstrings:

    WriteLine("Make me a Foo + {0} bars of {1}", 4 + 2, "goo");
    

    spoiler:

    method: WriteLine
    number of params: 3
    param1: "Make me a Foo + {0} bars of {1}"   (formatstring)
    params: 4 + 2                               (== 6, goes as {0})
    param3: "goo"                               (goes as {1})
    result: Make me a Foo + 6 bars of goo
    
    alternative way to get such text with extra parens for easier reading:
        ("Make me a Foo + ")  +  (4+2)  +  ("bars of ")  +  ("goo")
    

    So, the alternative code would be to take that formula and pass it as a whole to the WriteLine as a one huge parameter -- all in all, the formula produces a ready-to-use text, we need to just display it, no formatting needed:

    WriteLine( ("Make me a Foo + ")  +  (4+2)  +  ("bars of ")  +  ("goo") );
    // or
    WriteLine( "Make me a Foo + "  +  (4+2)  +  "bars of "  +  "goo" );
    

    it's hard to read, a temporary variable could help a bit:

    string text = "Make me a Foo + "  +  (4+2)  +  "bars of "  + "goo";
    WriteLine( text );
    

    That's all just ONE parameter for WriteLine now. No commas needed in such case.

    Note the 4+2 must be in parentheses, or else it'd be glued into the string as 4 then 2, not as four-plus-two. Fell free to try it out.

提交回复
热议问题