What is the Difference Between Console.Write() and Console.WriteLine() in C#?

In this tutorial, we will learn about the difference between Console.Write() and Console.WriteLine() in C#?
By IncludeHelp Last updated : April 06, 2023

Overview

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  . . .

Explanation

"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  . . .

Explanation

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




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.