Skip to main content
GameDev.net gamedev.net
🔒 Locked

C#,0 problem

Started by sevak Nov 20, 2004 at 7:35 PM 0 replies 450+ views
Original Post
sevak
sevak
ok I have this code. From what I understand the code below should cause the program to say Hello, CHECK [whatever u type] Welcome to C# Station Tutorial! but it actually says Hello, [whatever u type] CHECK Welcome to C# Station Tutorial! Can someone plz explain to me why? // Namespace Declaration using System; // Program start class class NamedWelcome { // Main begins program execution. public static void Main() { // Write to console Console.WriteLine("Hello, {0}!", "CHECK", Console.ReadLine()); Console.WriteLine("Welcome to the C# Station Tutorial!"); Console.ReadLine(); } }

www.computertutorials.org
computertutorials.org-all your computer needs...
kSquared
kSquared
Actually, the output is always going to be:

Hello, CHECK!
Welcome to the C# Station Tutorial!


When you specify parameter replacements in a WriteLine statement (that is, {0}, {1}, {2}, ...), each number corresponds to the index of the parameter. The second parameter you pass has index zero.

Console.WriteLine("Printing test strings: {0} {1}!", "foo", "bar");

The above prints "Printing test strings: foo bar!"

In your example, the {1} parameter is specified but never printed because you haven't included a "{1}" in the print string anywhere. Only the first parameter you passed ("CHECK") is printed since there's a {0} in the print string. What you probably want is this:

Console.WriteLine("Hello, {0} {1}!", "CHECK", Console.ReadLine());
- k2 "Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ<

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.