I wish to know the function of Console.Clear();. I have usual black colour console window. If Console.Clear(); was comment out output was blue colour XXXXX in black colour console window.
If Console.Clear(); was uncommented output was only yellow colour Console window no XXXXX at all.
using System;
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("XXXXX");
Console.BackgroundColor = ConsoleColor.Yellow;
Console.Clear();
Console.ReadKey();
}
}
Loading
Posted Sep 13, 2013, 9:33 AM
Suthish NairPosted Sep 13, 2013, 8:18 AM
Sanjeeb LenkaPosted Sep 13, 2013, 6:17 AM
Console.Clear(); only clears the text from the console window. it will not clear the color.
in your code if you comment the Console.Clear(); method it will show the XXXX in blue color and after that it will apply the background color. to check that try this example:
1st it will show XXXX in blue color then when you enter any thing it will show it background as yellow.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IfExample
{
class color
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("XXXXX");
Console.BackgroundColor = ConsoleColor.Yellow;
Console.ReadLine();
}
}
}
Next if you un comment the Console.Clear(); it will clear the text from the console window after that applied the back ground color.
Any doubt reply
Sunny SharmaPosted Sep 13, 2013, 6:09 AM
Console.Clear() wipes out everything on screen but keeping the properties set. Let me explain this:
"If Console.Clear(); was comment out output was blue colour XXXXX in black colour console window. "
Here you've set the Foreground color of Console as Blue already and that is active while you execute Console.WriteLine() method. By Default the Background color of console is black, so you are getting Blue Foreground ("XXXXX") and Black Background.
To answer your second concern:
"If Console.Clear(); was uncommented output was only yellow colour Console window no XXXXX at all. "
Like I explained, Console.Clear() wipes out everything on screen, no wonder how much and what data it is occupying, it will clear all the data on screen. it also clears the Console buffer off screen behind the scene.
You can imagine it painting the Wall again with the active Background Color.
After you've cleared the screen with your code above, if you try:
Console.WriteLine("Yellow Background");
it will definitely get printed on screen but in Blue, ecause the current Foreground color is set as Blue, so you'll get Blue text on Yellow background.
Hope I answered your question and you got the point :)
Interesting Question by the way.
Pavan RamamurthyPosted Sep 13, 2013, 6:08 AM
VulpesPosted Sep 13, 2013, 6:05 AM
So, if the background color is yellow, you'll just get a blank yellow screen.