locked
Console.WriteLine RRS feed

  • Question

  • in this code what's the meaning of {0}? Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
    Saturday, August 29, 2009 5:46 PM

Answers

  • Strings with {0} and {1}, etc are called format strings.   The {0} and {1}, etc calls are placeholders for the arguments that follow the string.  In this example, the result of "p * 1.1" will be calculated, and the string representation placed where the {0} resides, before outputting the entire string to the console.  

    Here's another example:

    Console.WriteLine("Hello, {0} my name is {1}", "Amir", "David");

    Would output:

    Hello, Amir my name is David

    to the console. 

    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiTwitterLinkedInForumsBrowser
    • Proposed as answer by AUmidh Saturday, August 29, 2009 6:06 PM
    • Marked as answer by David M Morton Saturday, August 29, 2009 6:42 PM
    Saturday, August 29, 2009 6:03 PM
  • Let's have some fun with David's example. 
    Add these two classes.  Add the method to your program.cs file.
    It demonstrates what occurs when that syntax is used.
    The ToString() method is called on the objects.

        class Amir
        {
            public override string ToString()
            {
                return "Amir";
            }
        }
        class David
        {
            public override string ToString()
            {
                return "David";
            }
        }

            private static void ConsoleWriteDemo()
            {
                David d = new David();
                Amir a = new Amir();
                Console.WriteLine("Hello, {0} my name is {1}", a, d);
            }


    Hope this helps.

    Rudedog   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by David M Morton Saturday, August 29, 2009 6:42 PM
    Saturday, August 29, 2009 6:29 PM

All replies

  • Strings with {0} and {1}, etc are called format strings.   The {0} and {1}, etc calls are placeholders for the arguments that follow the string.  In this example, the result of "p * 1.1" will be calculated, and the string representation placed where the {0} resides, before outputting the entire string to the console.  

    Here's another example:

    Console.WriteLine("Hello, {0} my name is {1}", "Amir", "David");

    Would output:

    Hello, Amir my name is David

    to the console. 

    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiTwitterLinkedInForumsBrowser
    • Proposed as answer by AUmidh Saturday, August 29, 2009 6:06 PM
    • Marked as answer by David M Morton Saturday, August 29, 2009 6:42 PM
    Saturday, August 29, 2009 6:03 PM
  • Hi David I understand thank you
    Saturday, August 29, 2009 6:27 PM
  • Let's have some fun with David's example. 
    Add these two classes.  Add the method to your program.cs file.
    It demonstrates what occurs when that syntax is used.
    The ToString() method is called on the objects.

        class Amir
        {
            public override string ToString()
            {
                return "Amir";
            }
        }
        class David
        {
            public override string ToString()
            {
                return "David";
            }
        }

            private static void ConsoleWriteDemo()
            {
                David d = new David();
                Amir a = new Amir();
                Console.WriteLine("Hello, {0} my name is {1}", a, d);
            }


    Hope this helps.

    Rudedog   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by David M Morton Saturday, August 29, 2009 6:42 PM
    Saturday, August 29, 2009 6:29 PM