Skip to content

ConsoleColor as Parameter (Console Application)

Using an object as a method parameter!

This short example shows how you can make a ConsoleColor object (myColor) and pass it as an argument to a method. The method uses the object to change text color. After a message is typed into the console window, it is printed in color. Then color is reset to the default properties.

Example Code

Colored text example: ConsoleColor as a method parameter
Colored text example
using System;

namespace ColorWriteLine
{
    class ColorWrite
    {
        static public void Run()
        {
            Console.Title = "ColorWriteLine";
            string input = "";
            ConsoleColor myColor = new ConsoleColor();
            myColor = ConsoleColor.Cyan;

            Console.WriteLine("What would you like written in color?\nType a message and press enter:");
            input = Console.ReadLine();

            ColorWriteLine(myColor, input);

            Console.WriteLine("Press enter to exit.");
            Console.ReadKey();
        }

        static void ColorWriteLine(ConsoleColor theColor, string theMessage)
        {
            Console.ForegroundColor = theColor;
            Console.WriteLine(theMessage);
            Console.ResetColor();
        }
    }
    class Program
    {
        static void Main()
        {
            ColorWrite.Run();

            Console.ReadKey();
        }
    }
}

For the list of color properties for ConsoleColor, see Console Application Color: Text and Background.