Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what is the meaning of {0}..pls can any one tell me importance of it......
C#
Console.WriteLine("Summed Values = {0}", sum);
Posted
Updated 4-May-12 20:19pm
v2

you need to know and learn formatting output with Console.WrilteLine
have a look at :
http://www.dotnetperls.com/console-writeline[^]
http://msdn.microsoft.com/en-us/library/txafckwd%28v=vs.71%29.aspx[^]
http://en.csharp-online.net/CSharp_Format_Specifiers%E2%80%94Numeric_Format_Specifiers[^]
and many more from Goolge search option , go and get from there
 
Share this answer
 
Comments
VJ Reddy 5-May-12 4:58am    
Good references. 5!
what is the meaning of {0}
{}: This tells it is one of those place where some string will be replaced when the line of code will be executed.
0: This tells that the string defined at 0th position in the order will be picked for replacement. So, it's the index to be picked for replacement.

For clear example:
C#
Console.WriteLine("Summed Values = {0}, Got it{1}", sum, mark); 

0 -> sum
1 -> mark

This kind of representation helps you to show something at runtime with ease. If you are not sure of certain values at runtime and want to collate few in a single output/statement, this syntax helps.
 
Share this answer
 
Comments
sravani.v 5-May-12 3:47am    
My 5!
Sandeep Mewara 5-May-12 4:43am    
Thanks Sravani.
VJ Reddy 5-May-12 4:58am    
Good answer. 5!
Sandeep Mewara 5-May-12 5:14am    
Thanks VJ.
Maheshdotnet2010 17-May-12 6:34am    
Thank you.........sir................
All the others have already provided the answer.
I just want to add that you can use this placeholder for formatting a string as well.

E.g. String.Format("The number {0} in Hexadecimal is {0:X}", 432);

Here is a set of formatters

Control	Description
C	Displays number prefixed with the currency simple appropriate to the current locale
D	Displays number in decimal form with optional padding
E	Displays number in scientific form with optional value for fractional part
E	Displays the number including the specified number of decimal digits
N	Converts a number to a human friendly format by inserting commas and rounding to the nearest 100th
X	Converts a number to hexadecimal
0:0...	Adds zeros to pad argument
0:0#...	Adds spaces to pad argument
%	Multiplies the argument by 100 and appends a percentage sign


(Table borrowed from http://www.techotopia.com/index.php/Formatting_Strings_in_C_Sharp[^]).
 
Share this answer
 
Comments
VJ Reddy 5-May-12 4:58am    
Good addition. 5!
Abhinav S 5-May-12 9:10am    
Thanks.
Maheshdotnet2010 17-May-12 6:34am    
Thank you sir.........
Abhinav S 17-May-12 23:47pm    
You are welcome. Vote if it helped.
The answers given are very good.

I want to add that the Field width and Field alignment can be given using the Format item {0},{1} etc. as shown below:
C#
Console.WriteLine("| {0,-20} | {1,20} |","Left Aligned","Right Aligned");
Console.WriteLine("| {0,-20} | {1,20:N2} |","Item1",20);
Console.WriteLine("| {0,-20} | {1,20:N2} |","Item2",50.545);
Console.WriteLine("| {0,-20} | {1,20:N2} |","Item3",30.396);

//| Left Aligned         |        Right Aligned |
//| Item1                |                20.00 |
//| Item2                |                50.55 |
//| Item3                |                30.40 |

-20 indicates left alignment with 20 characters width and 20 indicates right alignment with 20 characters width.

Of course to see the correct alignment and width a Monospaced font, like Courier New, explained here Monospaced font[^] is required.
 
Share this answer
 
Comments
Maheshdotnet2010 17-May-12 6:34am    
Thank you very much
Hi,

It's a place holder to hold the value in the memory.
C#
Console.WriteLine("Summed Values = {0}, {1}, {2}", sum, sum1, sum2);


You can also use multiple placeholder. It creates a array in the memory where values can be stored. Instead of using this you can also use concatenation operator like this:
C#
Console.WriteLine("Summed Values = "+ sum);


To print more than one values you can also use this like:
C#
Console.WriteLine("Summed Values = '"+ sum+"', '"+sum1+"'");


-AK
 
Share this answer
 
Comments
Maheshdotnet2010 17-May-12 6:35am    
Thank you so much
all above are correct. i would like to give explanation in different way.

syntax of writeline is :
Console.WriteLine("string to be printed");
but if u have any value of a variable to be printed then u need to provide it to WriteLine somehow.
for that v have another syntax:
Console.WriteLine("<string to="" be="" printed="">",<variables whose="" value="" is="" to="" be="" printed="">);

now the list contains variable names each separated by comma(,)
WriteLine identifies each variable in the list by its position i.e.
first variable in the list has index 0
second variable in the list has index 1

and so on.

but to print the value of the variable, it need to b included in the "string to be printed" section of WriteLine .
we provide this by using the index and to notify the method that it is index and not a character to be printed we provide the index within "{}"(braces)

so to print first variable in the list v provide index 0[i.e. we write {0} within "string to be printed"]
the value of variable will b printed in position where {} is positioned.
eg:-
C#
//a,b are variables
Console.WriteLine("{0} + {1} = {2}", a, b, (a+b));
// <value of a> + <value of b> = <value of (a+b)>
 
Share this answer
 
v2
Comments
Maheshdotnet2010 17-May-12 6:35am    
Thank you so much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900