Quick links: C-Tricks HR Interview Que. Code Snippets C Programs C++ Programs Java Programs PHP Examples

Home » .Net

What are the differences between Console.Write and Console.WriteLine in C#.Net?

C#.Net: Learn what are the differences between Console.Write and Console.WriteLine methods in C#.Net, why and when they are used?

In the last post we have learnt how to print our first program in C#.Net? In that program we used Console.WriteLine() to print the message on the console output screen, we can also use Console.Write() to do the same, let’s learn what are the differences between Console.Write and Console.WriteLine method in C#.Net?

Console.Write() and Console.WriteLine(), both methods are used to print data on console screen. Here, Console is a predefined class of System namespace from Framework Class Library, Write() and WriteLine() are the methods of the Console Class.

Console.Write()

Console.Write prints the data without printing the new line after the message.

Example of Console.Write() in C#.Net

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Hello World");
        }
    }
}

Output

Hello WorldPress any key to continue  . . .

"Press any key to continue..." is the default message after the output, you can see in the output there is no new line or even space after the program output "Hello World".

Console.WriteLine()

Console.WriteLine method prints the message on the console screen as well as new line character after the message.

Example of Console.WriteLine() in C#.Net

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
        }
    }
}

Output

Hello World
Press any key to continue  . . .

See the output, the message "Press any key to continue..." prints after the program output "Hello World".



Sponsored Links








COMMENTS