0

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.

Jeremy
  • 181
  • 1
  • 2
  • 10

4 Answers4

3

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

Console.WriteLine("Hello, " + name);
Console.WriteLine($"Hello, {name}");
Console.WriteLine(String.Format("Hello, {0}", name));
Alfie Goodacre
  • 2,753
  • 1
  • 13
  • 26
  • `WriteLine()` does have an appropriate overload, making the call to `Format()` unnecessary – Mathias R. Jessen Apr 01 '16 at 20:33
  • 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 01 '16 at 20:35
  • Thank you. I got confused because I remember using your third example earlier but forgot about the needed {#}. – Jeremy Apr 01 '16 at 20:45
2

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.

Riad Baghbanli
  • 3,105
  • 1
  • 12
  • 20
1

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, ".

The resulting output will be 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 :

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

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

  • Console.WriteLine(String, Object)
  • String Concatenation
  • On String Formatters
  • Literals vs Variables
  • Community
    • 1
    • 1
    Terrance
    • 11,764
    • 4
    • 54
    • 80
    1

    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.


    The second example:

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

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

    Jonesopolis
    • 25,034
    • 12
    • 68
    • 112