0
0
C Sharp (C#)programming~5 mins

Console.WriteLine and Write methods in C Sharp (C#)

Choose your learning style9 modes available
Introduction

These methods help you show messages or information on the screen when your program runs.

You want to show a message to the user.
You want to display the result of a calculation.
You want to print multiple pieces of information on the same line.
You want to print each piece of information on a new line.
You want to debug your program by checking values during execution.
Syntax
C Sharp (C#)
Console.WriteLine(value);
Console.Write(value);

WriteLine prints the value and moves to the next line.

Write prints the value but stays on the same line.

Examples
Prints "Hello World!" and moves to the next line.
C Sharp (C#)
Console.WriteLine("Hello World!");
Prints "Hello World!" on the same line because Write does not add a new line.
C Sharp (C#)
Console.Write("Hello ");
Console.Write("World!");
Prints the number 123 and moves to the next line.
C Sharp (C#)
Console.WriteLine(123);
Prints "Number: 456" with Write printing first part and WriteLine printing second part and moving to new line.
C Sharp (C#)
Console.Write("Number: ");
Console.WriteLine(456);
Sample Program

This program shows how Write and WriteLine work together. The first two lines print "Hello World!" with a new line after. Then it prints another line. Then it prints two numbers on the same line, followed by a new line.

C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        Console.Write("Hello ");
        Console.WriteLine("World!");
        Console.WriteLine("This is on a new line.");
        Console.Write(123);
        Console.Write(456);
        Console.WriteLine();
    }
}
OutputSuccess
Important Notes

Use WriteLine when you want to move to the next line automatically.

Use Write when you want to continue printing on the same line.

You can print text, numbers, or variables with these methods.

Summary

Console.WriteLine prints and moves to a new line.

Console.Write prints but stays on the same line.

Use these to show messages or data to the user.