Console.ForegroundColor Property

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets the foreground color of the console.

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

static member ForegroundColor : ConsoleColor with get, set

Property Value

Type: System.ConsoleColor

A ConsoleColor that specifies the foreground color of the console; that is, the color of each character that is displayed. The default is gray.

Exception Condition
ArgumentException

The color specified in a set operation is not a valid member of System.ConsoleColor.

SecurityException

The user does not have permission to perform this action.

IOException

An I/O error occurred.

A get operation for a Windows-based application, in which a console does not exist, returns Gray.

The following example checks whether the console's background color is black and, if it is, it changes the background color to red and the foreground color to black.

No code example is currently available or this language may not be supported.

The following example saves the values of the ConsoleColor enumeration to an array and stores the current values of the BackgroundColor and ForegroundColor properties to variables. It then changes the foreground color to each color in the ConsoleColor enumeration except to the color that matches the current background, and it changes the background color to each color in the ConsoleColor enumeration except to the color that matches the current foreground. (If the foreground color is the same as the background color, the text isn't visible.) Finally, it calls the ResetColor method to restore the original console colors.

No code example is currently available or this language may not be supported.

UIPermission

for modifying safe top-level windows and subwindows. Associated enumeration: UIPermissionWindow.SafeTopLevelWindows

.NET Framework
Available since 2.0
Return to top
Show: