tags:

views:

82

answers:

2

Why the FontFamily param of the Font object is a string and not an enum?

+3  A: 

Because an Enum is a set of fixed values that forces a re-compile when it changes (and in this case this would ultimately mean: a new release of the framework).

Font families are subject to change and available fonts differ from host system to host system.

Tomalak
+4  A: 

FontFamily refers to the name of the Font. While you could use "monospace" or "serif" I wouldn't think it would be supported by .Net.

Remember, using a enum would be impossible. A enum is a static compile-time feature, which means that it can't "generate" a enum dynamically from fonts on your system. Indeed, including anything this specific in a language would probably be a poor idea. Even if this was supported, the user's machine wouldn't have the same fonts as yours - some fonts would be incorrectly included in the list and some excluded (because once compiled an enum becomes 'final').

Enums are a convenient store of integral constants and NOTHING else. Each item in a enum has a convenient name and a value, even if you don't specify it. The following two enums are the same.

public enum MyEnum
{
  A = 1,
  B = 2
}

public enum FooEnum
{
  A,
  B
}

And there are two other problems, enum names can not contain spaces, where font names can. Getting the fields from an enum is not a trivial task (it requires a lot of reflection code).

The following code will get you a list of fonts (you will need to add System.Drawing as a reference):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing.Text;
    using System.Drawing;

    namespace ConsoleApplication19
    {
        class Program
        {
            static void Main(string[] args)
            {
                InstalledFontCollection ifc = new InstalledFontCollection();

                foreach (FontFamily o in ifc.Families)
                {
                    Console.WriteLine(o.Name);
                }
                Console.ReadLine();
            }
        }
    }
Jonathan C Dickinson