0

In C++ when you defined a variable and you want to print it you can just do

cout << "Your varibales is" << var1 << endl;

But why in c# you need a placeholder to do so?

Console.WriteLine("The answer is {0}" , answer);

Because i'm getting an error with printing the answer without the placeholder. I have searched on the web, but it doesn't give the information i need..

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
null
  • 1,062
  • 1
  • 9
  • 21
  • 1
    As an alternative to the answers below, you could also choose to use [interpolated strings](https://msdn.microsoft.com/en-us/library/dn961160.aspx) if you are using C# 6. It can make your strings more readable most of the time: `Console.WriteLine($"The answer is {answer}");` – Ilian Jan 09 '17 at 03:45

5 Answers5

3

It is not necessary you can do like this using concatenation subject to the condition that the variable must be a string , or else you have to use .ToString() for conversion and also to format the object:

Console.WriteLine("The answer is " + answer); // if answer is string

let answer be a DateTime object and you want to print the date only from that with the format "dd-MMM-yyyy" then you can use like this:

Console.WriteLine("The answer is " + answer.ToString("dd-MMM-yyyy")); // if answer is not string
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
2

Because this is the way how String.Format works. Console.WriteLine uses String.Format internally. You could write something like Console.WriteLine("The answer is " + answer); instead.

Diligent Key Presser
  • 4,183
  • 4
  • 26
  • 34
2

Placeholders are only used for string formatting: internally the WriteLine method will call the String.Format method, but you can either format it yourself, or use multiple Console.Write statements:

Console.Write("The answer is ");
Console.WriteLine(answer);

For instance is more or less equivalent to what you do in you C++ program since the statement:

cout << "Your varibales is" << var1 << endl;

basically boils down to:

cout2 = cout << "Your varibales is";
cout3 = cout2 << var1;
cout3 << endl;

and << on a cout is more or less equivalent to calling Write on the Console; the << simply returns the console object such that one can use chaining.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
1

When you try

Console.WriteLine("The answer is " , answer); //without placeholder

This wont give you error but would not print the answer and the console output would be The answer is since you havent told where to put the variable answer in here. So if you want to print the answer you can use either concatenate using + as suggested by other post or you have to use placeholder

Letz take an example to understand where to use what. Let say you have many variables to show in the output. You can use placeholder so that it would be easy to read. Such as

string fname = "Mohit";
string lname = "Shrivastava";
string myAddr = "Some Place";
string Designation = "Some Desig";

Now letz say we wanted to show some string on the output which is like

Hey!! Mohit whose last name would be Shrivastava is currently living at Some Place and he is working as Some Desig with so n so company.

Thus one of the way could be which is suggested by many of us.

Console.WriteLine("Hey!! " + fname + " whose last name would be " + lname + " is currently living at " + myAddr + " and he is working as " + Designation + " with so n so company.");

In this scenario Placeholder plays a vital role for better readability like

Console.WriteLine("Hey!! {0} whose last name would be {1} is currently living at {2} and he is working as {3} with so n so company.",fname,lname,myAddr,Designation);

With C#6.0 String Interpolation you can also do it in efficient way like

Console.WriteLine($"Hey!! {fname} whose last name would be {lname} is currently living at {myAddr} and he is working as {Designation} with so n so company.");
Community
  • 1
  • 1
Mohit S
  • 13,723
  • 6
  • 34
  • 69
1

You can use string interpolation in addition to these other answers:

Console.WriteLine($"The answer is {answer}");
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Rabid Penguin
  • 1,084
  • 9
  • 22