These methods help you show messages or information on the screen when your program runs.
Console.WriteLine and Write methods in 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.
Console.WriteLine("Hello World!");Console.Write("Hello "); Console.Write("World!");
Console.WriteLine(123);Console.Write("Number: "); Console.WriteLine(456);
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.
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(); } }
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.
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.
Practice
Console.WriteLine and Console.Write in C#?Solution
Step 1: Understand
Console.WriteLinebehaviorConsole.WriteLineprints the given text and then moves the cursor to the next line.Step 2: Understand
Console.WritebehaviorConsole.Writeprints the given text but keeps the cursor on the same line, so the next output continues on that line.Final Answer:
Console.WriteLineprints text and moves to a new line;Console.Writeprints text but stays on the same line. -> Option CQuick Check:
WriteLine adds newline, Write does not [OK]
- Confusing which method adds a newline
- Thinking WriteLine does not move to next line
- Assuming Write clears the console
Solution
Step 1: Check method usage with parentheses and quotes
In C#, method calls require parentheses and string arguments must be in double quotes.Step 2: Identify correct method for printing with newline
Console.WriteLine("Hello World");correctly prints the text and moves to the next line.Final Answer:
Console.WriteLine("Hello World"); -> Option DQuick Check:
Correct syntax uses parentheses and quotes [OK]
- Missing parentheses after method name
- Using square brackets instead of parentheses
- Omitting quotes around string
Console.Write("Hello ");
Console.WriteLine("World!");
Console.WriteLine("Goodbye!");Solution
Step 1: Analyze first line with Console.Write
Console.Write("Hello ");prints "Hello " and stays on the same line.Step 2: Analyze second line with Console.WriteLine
Console.WriteLine("World!");prints "World!" on the same line and then moves to the next line.Step 3: Analyze third line with Console.WriteLine
Console.WriteLine("Goodbye!");prints "Goodbye!" on a new line and moves to next line.Final Answer:
Hello World! Goodbye! -> Option BQuick Check:
Write stays, WriteLine adds newline [OK]
- Assuming WriteLine does not add newline
- Thinking Write moves to next line
- Confusing output line breaks
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}Solution
Step 1: Understand the difference between Write and WriteLine in loop
Console.WriteLine(i);prints the number and moves to a new line each time.Step 2: Fix by using Console.Write to stay on the same line
Replacing withConsole.Write(i);prints numbers continuously on the same line.Final Answer:
Use Console.Write instead of Console.WriteLine inside the loop. -> Option AQuick Check:
Write stays on line, WriteLine adds newline [OK]
- Using WriteLine when same line output is needed
- Adding unnecessary semicolons after for loop
- Confusing ReadLine with WriteLine
Solution
Step 1: Print numbers 1 to 3 on the same line with spaces
UsingConsole.Write(i + " ");prints each number followed by a space without moving to a new line.Step 2: Print "Done" on the next line
Console.WriteLine("Done");prints "Done" and moves to the next line.Final Answer:
for (int i = 1; i <= 3; i++) { Console.Write(i + " "); } Console.WriteLine("Done"); -> Option AQuick Check:
Write with space, then WriteLine for new line [OK]
- Using WriteLine inside loop causing multiple lines
- Not adding spaces between numbers
- Printing "Done" without newline
Ready to prove it?
Pass the test and this pattern moves unknown
