Hi Guys
NP131 StringBuilder/character type
This program is producing following result when ‘1’ in the Append() method is a character type (highlighted in yellow).
Please explain the reason.
myStringzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
Thank you
using System;
using System.Text;
class MainClass
{
public static void Main()
{
StringBuilder myStringBuilder = new StringBuilder();
myStringBuilder.Append("myString");
myStringBuilder.Append('z', '1');
Console.WriteLine(myStringBuilder);
}
}
AlanPosted Aug 23, 2008, 9:29 AM
The second argument to StringBuilder.Append is an int and determines how many times the first argument (a char) is repeated
However, when you pass the second argument a char, it is implicitly converted to an int and since '1' has a value of 49, 'z' is repeated 49 times.
Posted Aug 23, 2008, 9:35 AM
Thank you very much for explanation.