Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi...
Im new to programming. Im trying sample programs in console application.
While defining a program with bool datatype ,they provide sample code as
C#
public static void Main()
{
   bool content = true;
   bool noContent = false;

   Console.WriteLine("It is {0} that C# Station provides C# programming language content", content);
   Console.WriteLine("The statement above is not {0}", noContent);
}


But im having doubt why cant give as
C#
console.writeline("The statement above is not {1}",nContent);


I tried this and got error as "Index (zero based) must be greater than or equal to zero and less than the size of the argument list."
But not clear in error...guide me
Posted
Updated 15-Jun-12 3:03am
v2

The reason you are getting that error is because you put a {1} in the Console.WriteLine without having a {0}. You need to start from zero, since it is a zero-based index.

The numbers inside the curly braces represent the values you are going to pass into the string at runtime. For each unique number inside the string, you have to have a variable outside the string that will fill that space. These unique numbers have to start from zero and they have to be sequential. That does not mean that they have to be in order inside your string, just that you can't skip numbers. Here is an example:
C#
Console.WriteLine("This is the {1} variable and this is the {0}", "first", "second");

This line will print out "This is the second variable and this is the first". I specified two variables and I mixed up the order, but it is perfectly fine because I have a zero and I have a one. The zero corresponds to the first variable passed in after my string and the one corresponds to the second variable I passed in after my string.
 
Share this answer
 
v2
The 0 in your format string tells the formatter which parameter to use when building the resulting string. Parameters are indexed from 0 upwards starting with the first one after the string. Since you only have one parameter in both statements it is always at index position 0, you do not have one at position 1

You could write something like:
C++
Console.WriteLine("The parameters are {1} and {0}", noContent, content);

to demonstrate how it selects them.
 
Share this answer
 

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