Why do commas not work to include strings in WriteLine?

Why do commas not work to include strings in WriteLine?



I am wondering why a '+' is needed in a scenario like this:


string name = "Rick";
Console.WriteLine("Hello, " + name);



I was under the impression that the '+' was needed to include a variable of another type, and a string could be included without one. Is this incorrect?



Further, if that is the case, then why does it simply skip the variable and not return an error in a situation like this:


string name = "Rick";
Console.WriteLine("Hello, " , name);



Thanks.





Console.WriteLine("Hello, 0" , name);
– James R.
Apr 1 '16 at 20:27




4 Answers
4



As far as the compiler is concerned, the second version is trying to pass a second parameter, which the WriteLine method is not overloaded for. The options you really have for this are as follows


WriteLine


Console.WriteLine("Hello, " + name);
Console.WriteLine($"Hello, name");
Console.WriteLine(String.Format("Hello, 0", name));





WriteLine() does have an appropriate overload, making the call to Format() unnecessary
– Mathias R. Jessen
Apr 1 '16 at 20:33


WriteLine()


Format()





Ah right you are, though as the OP seems to be unsure about string concatenation so it might be worth leaving it in in case he didn't know of its existence :)
– Alfie Goodacre
Apr 1 '16 at 20:35





Thank you. I got confused because I remember using your third example earlier but forgot about the needed #.
– Jeremy
Apr 1 '16 at 20:45



C# concatenates string using operator +. So, one strings adds to another string creating third string containing first and second strings. Comma does not concatenate strings, comma separates parameters.


+



You're getting confused. in your first example:


Console.WriteLine("Hello, " + name);



the two strings are being concatenated, and the result is being passed as a parameter to the Console.WriteLine method.


Console.WriteLine



The second example:


Console.WriteLine("Hello, " , name);



is actually calling a different overload of that method, that accepts a string and an object.



The first Example:


string name = "Rick"; //string variable
Console.WriteLine("Hello, " + name);



the + operator is used to concatenate the value of string variable name and the string literal "Hello, ".


+


name


"Hello, "



The resulting output will be Hello, Rick


Hello, Rick



For more see:String Concatenation



The second example:


string name = "Rick";
Console.WriteLine("Hello, " , name);



Matches the overload
Console.WriteLine(String, Object)



It requires the use of a formatter 0 because the formatter is not present nothing will happen with the second argument Object (in your case in the variable name). A proper version of this would be written as :


0


Object


name



string name = "Rick";
Console.WriteLine("Hello, 0", name);


string name = "Rick";
Console.WriteLine("Hello, 0", name);




The object is cast to string and creates a new string with the value of name replacing the 0 value.


The resulting output that is written to screen would be:Hello, Rick


0


Hello, Rick



For more info I'd recommend you look at the following:






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ