January 2, 2014

Displaying Curly Braces { } in Console ouptut

Recently I was working on a sample console application for my tutorial and I happen to have a requirement, where in I had to display the curly braces as part of the formatted output.
Here’s the example ; Output of the console program should be: The number is -{124}
Solution:
As you are completely aware that curly braces is part of the c# keywords, for formatting object (e.g. numeric or string or class object etc.)
{
string x = “124″;
Console.WriteLine(“The number is -{x}”, x);
}
output : The number is -124
in the above example the output is still missing the curly braces, the solution to this is adding twice open and close {{ }} curly braces to get the desired output. Note is only for curely braces for all other special characters you need to use ‘\’
{
string x = “124″;
Console.WriteLine(“The number is -{{x}}”, x);
}
output : The number is -{124}
Thanks,
Hussain Patel

No comments:

Post a Comment