Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, May 17, 2014 6:34 AM
Hi i am sunil , i just started learning C# Programming .. while i was trying simple array program and printing the values.i dont clearly understand the meaning of this print statement .
/// Console.WriteLine("Element[{0}] = {1}", j, n[j]);
can'nt we just write this statement like this for printing the array values
Console.WriteLine("Element are " , n[j]);
if not then plz.. explain........Thanks in advance.. complete program is written belowusing System;
namespace ArrayApplication
{
class MyArray
{
static void Main(string[] args)
{
int [] n = new int[10]; /* n is an array of 10 integers */
int i,j;
/* initialize elements of array n */
for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100;
}
/* output each array element's value */
for (j = 0; j < 10; j++ )
{
Console.WriteLine("Element[{0}] = {1}", j, n[j]);
}
Console.ReadKey();
}
}
}
All replies (2)
Saturday, May 17, 2014 6:54 AM âś…Answered
In the line below you have 3 objects
1) The format : "Element[{0}] = {1}"
2) Parameter 0 : j - Replaces the {0} in the format
3) Parameter 1 : n[j] - Replaces the {1} in the format
Console.WriteLine("Element[{0}] = {1}", j, n[j]);
jdweng
Saturday, May 17, 2014 6:55 AM
Have your ran your code that you suggest? "Elemet are", n[j]
That gives nothing but following output:
Elements are
Elements are
Elements are
.
.. 7 more times
Back to your original question:
heres hes storing the positional values, value of j goes to {0} or 0th argument and n[j] goes to {1} or 1st argument.
You may add even more things like , ("Element[{0}] = {1} ${2}",j,n[j],chand)
This will added chand in end of all prints.
Another thing that depicts here is that, Array starts from 0 not 1, value of 0th element is 100 (which is 0+100) and so on...