Wednesday 19 August 2015

What is difference between Write() and WriteLine() methods in .Net

Write() Method will keep the cursor on the same Line after writing the data.

     Example of Write() Method :
                    Public class demo
                     {
                            public static void main()
                            {
                              // Print 1 to 10 numbers
                               for(int i=1; i <=10 ; i++)
                                {
                                  Console.Write( i + " ");                              
                                }                        
                     }

        Out Put :  1 2 3 34 5 6 7 8 9 10

  Explanation : The above program is displaying the 1 to 10 numbers. it will be displaying one after another number but not next line and cursor is same line.   
             

WriteLine() Method will take cursor to new Line.

    Example of WriteLine() Method :
                    Public class demo
                     {
                            public static void main()
                            {
                              // Print 1 to 10 numbers
                               for(int i=1; i <=10 ; i++)
                                {
                                  Console.WriteLine( i + " ");                              
                                }                        
                     }

        Out Put :  1
                         2
                         3
                         4
                         5
                         6
                         7
                         8
                         9
                        10

  Explanation : The above program is displaying the 1 to 10 numbers. it shows one number for one line and once it print the cursor will move the next line.

No comments:

Post a Comment