I'm writing a code that asks a user for two initials and if they can be converted to chars, prints out the initials. However, I'm finding some strange behavior. Why is it that this code prints out two initials (chars) as expected:
Console.WriteLine(" please put in your first and last initial :");
string firstinitial = Console.ReadLine();
string lastinitial= Console.ReadLine();
char firstin1;
char lastin1;
if (!char.TryParse(firstinitial, out firstin1))
{
Console.WriteLine("not valid");
}
if (!char.TryParse(lastinitial, out lastin1))
{
Console.WriteLine("not valid");
}
Console.WriteLine( firstin1 + " " + lastin1);
Console.ReadKey();
and THIS code prints out a number?
Console.WriteLine(" please put in your first and last initial :");
string firstinitial = Console.ReadLine();
string lastinitial= Console.ReadLine();
char firstin1;
char lastin1;
if (!char.TryParse(firstinitial, out firstin1))
{
Console.WriteLine("not valid");
}
if (!char.TryParse(lastinitial, out lastin1))
{
Console.WriteLine("not valid");
}
Console.WriteLine(firstin1 + lastin1);
Console.ReadKey();
Obviously the + between two chars is the problem, but why? Is it based on how chars are stored in C#?