Open E-School

Male
Friday, February 24, 1984

Console.BackgroundColor – Change console background color using C#

Category: Programming Date: Monday, March 23, 2015

Goal – Learn how to change the background color for the console window using C#

Namespaces used

System ( Console class is defined in the System namespace )

Classes used

Console

Properties used

Console.BackgroundColor – Static property defined in the Console class

Brief description

You can change the console background color using Console.BackgroundColor property in C#. But when you use this property you will notice that it only changes the background color of the text which you write on console. But you might think that it will change the background color of the full console window. There is a way to change the background color of the full console window and we will discuss this method in this article.

Code

using System;    namespace ConsoleApplication1  {      class Program      {          public static void Main(string[] args)          {              Console.BackgroundColor = ConsoleColor.Red;              Console.WriteLine("Hello World");          }      }  }


Output

Console.BackgroundColor Output - 1
 
Explanation of the code

In the first line of code we have set the Console.BackgroundColor to red. You might think that it will change the full console background color to red but what you notice is that it only changes the background color of the text and makes it red.

Let’s change some code

using System;    namespace ConsoleApplication1  {      class Program      {          public static void Main(string[] args)          {              Console.BackgroundColor = ConsoleColor.Red;              Console.Clear();              Console.WriteLine("Hello World");          }      }  }

Output

Console.BackgroundColor Output - 2
 
Explanation of the code

We have made a slight change in the first code block and added Console.Clear() just after setting the Console.BackgroundColor to red. Doing this will change the background color of the whole console window to red as compared to the first example where only the background color of the text changes to red.

Extra Remarks

Console.BackgroundColor property is an enum type property. You cannot give it any color you want. You will have to choose a color you want to set as the Console background from the ConsoleColor enum.

Rate this article and help us improve

Please Login to rate
Overall ratings: 0 | Rating: out of 5
Previous Article Next Article

Posted By

Open E-School
Member since : 2015-01-09
Total points : 3375

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp