FontFamily question

Why is the FontFamily parameter of the Font object a string and not an enumeration?

0


source to share


2 answers


FontFamily refers to the name of the font. While you could have used "monospace" or "serif", I wouldn't have thought it would be supported by .Net.

Remember, using an enumeration would not be possible. An enumeration is a compile-time static function, which means that it cannot "generate" a dynamic enumeration from fonts on your system. Indeed, including something like this in language would probably be a bad idea. Even if this were confirmed, the user's machine would not have the same fonts as yours - some fonts would be incorrectly listed and some excluded (because the enum becomes "final" after compilation).

Enumerations are a convenient storage for integral constants and NOTHING else. Each element in the enumeration has a friendly name and meaning, even if you don't specify it. The next two listings are the same.

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

public enum FooEnum
{
  A,
  B
}

      



And there are two more issues, enumeration names cannot contain spaces where font names can be. Retrieving fields from an enumeration is not a trivial task (it requires a lot of reflection code).

The following code will give 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();
            }
        }
    }

      

+4


source


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



The font family is subject to change and the available fonts differ from host system to host system.

+3


source







All Articles