While debugging my application, I came across a behavior I'm puzzled by. I have the following
foreach (var customer in _dbContext.Customer)
{
Debug.WriteLine("Customer Name: {0}", customer.Name); // The output was not what I expected.
...
}
Actual Output
Peter:Customer Name: {0}
However, if I rewrite the statement to this.
foreach (var customer in _dbContext.Customer)
{
Debug.WriteLine("Customer Name: " + customer.Name);
...
}
Actual Output
Customer Name: Peter
I added the following the code to the same file to see why my original code wasn't working.
string first = "Peter";
string last = "Piper";
string what = "pick";
Debug.WriteLine("1 {0} 2 {1}, 3 {0} 4 {1}, 5 {2}.", first, last, what);
Actual Output
1 Peter 2 Piper, 3 Peter 4 Piper, 5 pick.
I am not sure why Debug.WriteLine("Customer Name: {0}", customer.Name); would output this CusPeter:Customer Name: {0}
Many Thanks


Debug.WriteLine("Customer Name: {0}", (object)customer.Name)instead