
The Font type represents a typeface in the .NET Framework and C# programs. You can create an instance of the Font type with its constructor, and then use the reference returned to specify the typefaces used in your programs.
This example is a Windows Forms program and its uses the simplest Font constructor. We specify a font name ("Times New Roman") as the first argument; the second argument is of type float and specifies the size. If you get an error about the number format of the second argument, use the "f" suffix to instruct the compiler that the number should be a float.
Suffix Examples, Numeric SuffixesThis C# program shows how to use the Font type. It requires Windows Forms.
Example that creates Font instance [C#]
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Font font = new Font("Times New Roman", 12.0f);
// Set Font property and then add a new Label.
this.Font = font;
this.Controls.Add(new Label() { Text = "Dot Net Perls" });
this.Size = new Size(300, 200);
}
}
}Results. The result of the above program is shown in the screenshot. A new label that inherits the Font created in the program is shown. The well-known Times New Roman type face is used to render the text.

Next we demonstrate a more complex constructor for the Font type. We introduce the FontFamily type; this type describes a specific family of fonts, such as the "Times New Roman" family. No style, size, or other information is part of the FontFamily. In the Font constructor, we specify the FontStyle using the bitwise OR operator. This is used on enums with the [Flags] attribute. The program renders a Label with "Times New Roman" that is bold, italic, and underlined at 16 points.
Enum Flags Attribute
Example that uses another Font constructor [C#]
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
FontFamily family = new FontFamily("Times New Roman");
Font font = new Font(family, 16.0f,
FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);
// Set Font property.
this.Font = font;
this.Controls.Add(new Label() { Text = "Dot Net Perls", Width = 250 });
this.Size = new Size(300, 200);
}
}
}The Font type also provides the instance properties Bold, Italic, and Underline. These properties are read-only, which means you can access them to determine the styles set on the Font, but can't change the styles. You must instead use the Font constructor as shown in the previous example.
The Style property on the Font type is also read-only. It will tell you what styles are set on the Font instance, but you cannot mutate them. Instead, create a new Font with the constructor.
The FontFamily type is useful because it represents families of fonts separately from their styles. This means that if you use the font "Times New Roman" in several different sizes and styles in your program, you only need one FontFamily instance for all of them. This can reduce bugs caused by trying to store all the family information in strings only.

The Font type is very useful in graphical programs in the .NET Framework and C# language. It cannot represent colors of fonts, but it does store information about styles, sizes, and type faces. You can combine the Font type with a FontFamily instance that helps separate parts of your Font requirements.
Drawing Instructions