We can enumerate colors in the C# language. We build a table containing all named colors, making HTML easier to write.
With a foreach
loop, we can enumerate the values in an enum
like the type KnownColor
. We can write the color table directly to the console.
We use the Enum.GetNames
method and the typeof
operator along with the Where extension method. With Where, we remove Control colors from the output.
System.Drawing
assembly might be needed.StartsWith
method and LINQ extension methods. These help generate the correct output.using System; using System.Drawing; using System.Linq; class Program { static void Main() { // Get enum strings and order them by name. // Remove Control colors. foreach (string c in Enum.GetNames(typeof(KnownColor)).Where( item => !item.StartsWith("Control")).OrderBy(item => item)) { // Write table row. Console.WriteLine("<b style=background:{0}>{1}</b>", c.ToLower(), c.ToLower().PadRight(20)); } } }
In this example, the KnownColors
can be enumerated
and listed. The C# code also helps us learn about how to use this programming language.
We can enumerate KnownColors
from the System.Drawing
namespace. Next we saw the console program that generates the table. A color sheet can be useful for selecting named colors.