Friday, October 23, 2015

Difference between Response.Write and Response.WriteLine

Introduction:

In this post we will discuss about the difference between Response.Write and Response.WriteLine in C#. These are the methods we used to show the output in console application. Both these methods come from class Console under namespace System. Behavior of both these are as same as except only one thing.

So what is the difference then?

The only difference is the position of the cursor. In the Write() method the after the output has been printed the cursor remains in the same line, But in case of WriteLine() the cursor shifted to the next line, that means you don't need to worry about the next line printing in WriteLine(). You have to use a new line after using Write() method if you want to continue with the next line. To do so you can use System.Environment.NewLine.

Using Console.Write():

public void Method2()
{
    // Keep the cursor in that line only.
    Console.Write("Hi this is Console.Write. ");

    // Keep the cursor on the next line.
    Console.Write("Hi this is Console.Write. "+ System.Environment.NewLine);
}

Output:


Using Console.WriteLine():

public void Method3()
{
    // Keep the cursor on the next line.
    Console.WriteLine("Hi this is Console.WriteLine. ");
}

Output:


Check the cursor position in both of the output. In the first image the cursor is in the same line and in the second output the cursor is in the next line.


0 comments:

Post a Comment

Popular Posts

Pageviews