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