Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
 ConsoleColor Enumeration
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
ConsoleColor Enumeration

Updated: November 2007

Specifies constants that define foreground and background colors for the console.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Visual Basic (Declaration)
<SerializableAttribute> _
Public Enumeration ConsoleColor
Visual Basic (Usage)
Dim instance As ConsoleColor
C#
[SerializableAttribute]
public enum ConsoleColor
Visual C++
[SerializableAttribute]
public enum class ConsoleColor
J#
/** @attribute SerializableAttribute */ 
public enum ConsoleColor
JScript
public enum ConsoleColor
Member nameDescription
Black The color black.
DarkBlue The color dark blue.
DarkGreen The color dark green.
DarkCyan The color dark cyan (dark blue-green).
DarkRed The color dark red.
DarkMagenta The color dark magenta (dark purplish-red).
DarkYellow The color dark yellow (ochre).
Gray The color gray.
DarkGray The color dark gray.
Blue The color blue.
Green The color green.
Cyan The color cyan (blue-green).
Red The color red.
Magenta The color magenta (purplish-red).
Yellow The color yellow.
White The color white.

The following code example demonstrates the System..::.ConsoleColor enumeration in conjunction with the Console..::.ForegroundColor and Console..::.BackgroundColor properties, and the Console..::.ResetColor method. You must run the example on a console to see the described color effects.

Two constants, Black and White, are used directly; then all the constants (Blue, DarkRed, and so on) are used indirectly in a loop. First, the names of the constants are obtained by using the GetNames method, which is inherited from the Enum class. Then the Enum..::.Parse method uses each name to create the corresponding enumerated constant.

Visual Basic
' This example demonstrates the ConsoleColor enumeration.
Imports System

Class Sample
   Public Shared Sub Main()
      Dim nl As [String] = Environment.NewLine
      Dim colorNames As [String]() = ConsoleColor.GetNames(GetType(ConsoleColor))
      Dim x As Integer

      ' ---------------------------------------------------------------------------------------
      Console.WriteLine("{0}All the foreground colors on a constant black background.", nl)
      Console.WriteLine("  (Black on black is not readable.){0}", nl)

      For x = 0 To colorNames.Length - 1
         Console.Write("{0,2}: ", x)
         Console.BackgroundColor = ConsoleColor.Black
         Console.ForegroundColor = CType([Enum].Parse(GetType(ConsoleColor), _
                                                colorNames(x)), ConsoleColor)
         Console.Write("This is foreground color {0}.", colorNames(x))
         Console.ResetColor()
         Console.WriteLine()
      Next x
      ' ---------------------------------------------------------------------------------------
      Console.WriteLine("{0}A constant white foreground on all the background colors.", nl)
      Console.WriteLine("  (White on white is not readable.){0}", nl)

      For x = 0 To colorNames.Length - 1
         Console.Write("{0,2}: ", x)
         Console.ForegroundColor = ConsoleColor.White
         Console.BackgroundColor = CType([Enum].Parse(GetType(ConsoleColor), _
                                                colorNames(x)), ConsoleColor)
         Console.Write("This is background color {0}.", colorNames(x))
         Console.ResetColor()
         Console.WriteLine()
      Next x
      ' ---------------------------------------------------------------------------------------
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'All the foreground colors on a constant black background.
'  (Black on black is not readable.)
'
' 0: This is foreground color Black.
' 1: This is foreground color DarkBlue.
' 2: This is foreground color DarkGreen.
' 3: This is foreground color DarkCyan.
' 4: This is foreground color DarkRed.
' 5: This is foreground color DarkMagenta.
' 6: This is foreground color DarkYellow.
' 7: This is foreground color Gray.
' 8: This is foreground color DarkGray.
' 9: This is foreground color Blue.
'10: This is foreground color Green.
'11: This is foreground color Cyan.
'12: This is foreground color Red.
'13: This is foreground color Magenta.
'14: This is foreground color Yellow.
'15: This is foreground color White.
'
'A constant white foreground on all the background colors.
'  (White on white is not readable.)
'
' 0: This is background color Black.
' 1: This is background color DarkBlue.
' 2: This is background color DarkGreen.
' 3: This is background color DarkCyan.
' 4: This is background color DarkRed.
' 5: This is background color DarkMagenta.
' 6: This is background color DarkYellow.
' 7: This is background color Gray.
' 8: This is background color DarkGray.
' 9: This is background color Blue.
'10: This is background color Green.
'11: This is background color Cyan.
'12: This is background color Red.
'13: This is background color Magenta.
'14: This is background color Yellow.
'15: This is background color White.
'

C#
// This example demonstrates the ConsoleColor enumeration.
using System;

class Sample 
{
    public static void Main() 
    {
    String nl = Environment.NewLine;
    String[] colorNames = ConsoleColor.GetNames(typeof(ConsoleColor));

// ---------------------------------------------------------------------------------------
    Console.WriteLine("{0}All the foreground colors on a constant black background.", nl);
    Console.WriteLine("  (Black on black is not readable.){0}", nl);

    for (int x = 0; x < colorNames.Length; x++)
    {
    Console.Write("{0,2}: ", x);
    Console.BackgroundColor = ConsoleColor.Black;
    Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[x]);
    Console.Write("This is foreground color {0}.", colorNames[x]);
    Console.ResetColor();
    Console.WriteLine();
    }
// ---------------------------------------------------------------------------------------
    Console.WriteLine("{0}A constant white foreground on all the background colors.", nl);
    Console.WriteLine("  (White on white is not readable.){0}", nl);

    for (int x = 0; x < colorNames.Length; x++)
    {
    Console.Write("{0,2}: ", x);
    Console.ForegroundColor = ConsoleColor.White;
    Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorNames[x]);
    Console.Write("This is background color {0}.", colorNames[x]);
    Console.ResetColor();
    Console.WriteLine();
    }
// ---------------------------------------------------------------------------------------
    }
}
/*
This example produces the following results:

All the foreground colors on a constant black background.
  (Black on black is not readable.)

 0: This is foreground color Black.
 1: This is foreground color DarkBlue.
 2: This is foreground color DarkGreen.
 3: This is foreground color DarkCyan.
 4: This is foreground color DarkRed.
 5: This is foreground color DarkMagenta.
 6: This is foreground color DarkYellow.
 7: This is foreground color Gray.
 8: This is foreground color DarkGray.
 9: This is foreground color Blue.
10: This is foreground color Green.
11: This is foreground color Cyan.
12: This is foreground color Red.
13: This is foreground color Magenta.
14: This is foreground color Yellow.
15: This is foreground color White.

A constant white foreground on all the background colors.
  (White on white is not readable.)

 0: This is background color Black.
 1: This is background color DarkBlue.
 2: This is background color DarkGreen.
 3: This is background color DarkCyan.
 4: This is background color DarkRed.
 5: This is background color DarkMagenta.
 6: This is background color DarkYellow.
 7: This is background color Gray.
 8: This is background color DarkGray.
 9: This is background color Blue.
10: This is background color Green.
11: This is background color Cyan.
12: This is background color Red.
13: This is background color Magenta.
14: This is background color Yellow.
15: This is background color White.
*/

Visual C++
// This example demonstrates the ConsoleColor enumeration.
using namespace System;
int main()
{
   String^ nl = Environment::NewLine;
   array<String^>^colorNames = Enum::GetNames( ConsoleColor::typeid );

   // ---------------------------------------------------------------------------------------
   Console::WriteLine( "{0}All the foreground colors on a constant black background.", nl );
   Console::WriteLine( "  (Black on black is not readable.){0}", nl );
   for ( int x = 0; x < colorNames->Length; x++ )
   {
      Console::Write( "{0,2}: ", x );
      Console::BackgroundColor = ConsoleColor::Black;
      Console::ForegroundColor =  *dynamic_cast<ConsoleColor^>(Enum::Parse( ConsoleColor::typeid, colorNames[ x ] ));
      Console::Write( "This is foreground color {0}.", colorNames[ x ] );
      Console::ResetColor();
      Console::WriteLine();
   }
   Console::WriteLine( "{0}A constant white foreground on all the background colors.", nl );
   Console::WriteLine( "  (White on white is not readable.){0}", nl );
   for ( int x = 0; x < colorNames->Length; x++ )
   {
      Console::Write( "{0,2}: ", x );
      Console::ForegroundColor = ConsoleColor::White;
      Console::BackgroundColor =  *dynamic_cast<ConsoleColor^>(Enum::Parse( ConsoleColor::typeid, colorNames[ x ] ));
      Console::Write( "This is background color {0}.", colorNames[ x ] );
      Console::ResetColor();
      Console::WriteLine();

   }
}

/*
This example produces the following results:

All the foreground colors on a constant black background.
(Black on black is not readable.)

0: This is foreground color Black.
1: This is foreground color DarkBlue.
2: This is foreground color DarkGreen.
3: This is foreground color DarkCyan.
4: This is foreground color DarkRed.
5: This is foreground color DarkMagenta.
6: This is foreground color DarkYellow.
7: This is foreground color Gray.
8: This is foreground color DarkGray.
9: This is foreground color Blue.
10: This is foreground color Green.
11: This is foreground color Cyan.
12: This is foreground color Red.
13: This is foreground color Magenta.
14: This is foreground color Yellow.
15: This is foreground color White.

A constant white foreground on all the background colors.
(White on white is not readable.)

0: This is background color Black.
1: This is background color DarkBlue.
2: This is background color DarkGreen.
3: This is background color DarkCyan.
4: This is background color DarkRed.
5: This is background color DarkMagenta.
6: This is background color DarkYellow.
7: This is background color Gray.
8: This is background color DarkGray.
9: This is background color Blue.
10: This is background color Green.
11: This is background color Cyan.
12: This is background color Red.
13: This is background color Magenta.
14: This is background color Yellow.
15: This is background color White.
*/

J#
// This example demonstrates the ConsoleColor enumeration.
import System.*;

class Sample
{
    public static void main(String[] args)
    {
        String nl = Environment.get_NewLine();
        String colorNames[] = ConsoleColor.GetNames(ConsoleColor.class.ToType());
        // ----------------------------------------------------------------------
        Console.WriteLine("{0}All the foreground colors on a constant black "
            + " background.", nl);
        Console.WriteLine("  (Black on black is not readable.){0}", nl);

        for (int x = 0; x < colorNames.get_Length(); x++) {
            Console.Write("{0,2}: ", System.Convert.ToString(x));
            Console.set_BackgroundColor(ConsoleColor.Black);
            Console.set_ForegroundColor(((ConsoleColor)(Enum.Parse(
                ConsoleColor.class.ToType(), System.Convert.ToString(
                colorNames.get_Item(x))))));
            Console.Write("This is foreground color {0}.",
                colorNames.get_Item(x));
            Console.ResetColor();
            Console.WriteLine();
        }
        // ----------------------------------------------------------------------
        Console.WriteLine("{0}A constant white foreground on all the "
            + "background colors.", nl);
        Console.WriteLine("  (White on white is not readable.){0}", nl);

        for (int x = 0; x < colorNames.get_Length(); x++) {
            Console.Write("{0,2}: ", System.Convert.ToString(x));
            Console.set_ForegroundColor(ConsoleColor.White);
            Console.set_BackgroundColor(((ConsoleColor)(Enum.Parse(
                ConsoleColor.class.ToType(), 
                System.Convert.ToString(colorNames.get_Item(x))))));
            Console.Write("This is background color {0}.", 
                colorNames.get_Item(x));
            Console.ResetColor();
            Console.WriteLine();
        }
        // ----------------------------------------------------------------------
    } //main
} //Sample
/*
This example produces the following results:

All the foreground colors on a constant black background.
  (Black on black is not readable.)

 0: This is foreground color Black.
 1: This is foreground color DarkBlue.
 2: This is foreground color DarkGreen.
 3: This is foreground color DarkCyan.
 4: This is foreground color DarkRed.
 5: This is foreground color DarkMagenta.
 6: This is foreground color DarkYellow.
 7: This is foreground color Gray.
 8: This is foreground color DarkGray.
 9: This is foreground color Blue.
10: This is foreground color Green.
11: This is foreground color Cyan.
12: This is foreground color Red.
13: This is foreground color Magenta.
14: This is foreground color Yellow.
15: This is foreground color White.

A constant white foreground on all the background colors.
  (White on white is not readable.)

 0: This is background color Black.
 1: This is background color DarkBlue.
 2: This is background color DarkGreen.
 3: This is background color DarkCyan.
 4: This is background color DarkRed.
 5: This is background color DarkMagenta.
 6: This is background color DarkYellow.
 7: This is background color Gray.
 8: This is background color DarkGray.
 9: This is background color Blue.
10: This is background color Green.
11: This is background color Cyan.
12: This is background color Red.
13: This is background color Magenta.
14: This is background color Yellow.
15: This is background color White.
*/

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Code example correction      samegutt   |   Edit   |  
Line 2 in the Main method should read
String[] colorNames = Enum.GetNames(typeof(ConsoleColor));
SS
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker