Color table. We can enumerate colors in the C# language. We build a table containing all named colors, making HTML easier to write.
Some notes. 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.
Example. 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.
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));
}
}
}
Discussion. In this example, the KnownColors can be enumerated and listed. The C# code also helps us learn about how to use this programming language.
Summary. 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.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Sep 24, 2022 (edit link).