Thursday, September 16, 2010

Console Formatting

by Emmaneale Mendu

Hi guys in this post we will learn about printing or formatting capabilities of the WriteLine function.
class Program
    {
        static void Main(string[] args)
        {
            Hello();

            Console.ReadKey();
        }

        ///
        /// Welcome Message
        ///
        static void Hello()
        {    
            Console.WriteLine("Hello {0}!", "World");
        }    
    }

OutPutHello World!

The zero in the curly braces means that first comma there is some value and that it should display this value. In the same way if you have two or more it can work as shown below.

class Program
    {
        static void Main(string[] args)
        {
            Hello();

            Console.ReadKey();
        }

        ///
        /// Welcome Message
        ///
        static void Hello()
        {    
            Console.WriteLine("Numbers {0},{1},{2},{3}!",100,200,300,400);
        }    
    }

OutPut
Numbers 100,200,300,400!

Here the {0} is replaced with 100 and {1} is replaced with 200 and so on.....

Play with Boolean now.

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("{0}", 5 > 2);
            Console.WriteLine("{0}", 5 < 2);
            Console.WriteLine("{0}", 2 < 2);
            Console.WriteLine("{0}", 2 <= 2);

            Console.ReadKey();
        }
    }

OutPut
True
False
False
True

No comments:

Post a Comment