Using Static Classes

Problem
You want to know when to make a class static.

Solution
Use a static class for types that you don’t want instantiated or inherited from.

Comments
A static class is a sealed class with a private constructor and only static members. Static classes are usually used to accomplish one of the following objectives:

1. To implement the Singleton design pattern.
2. To act as a container for methods.
3. To act as a container for an application’s global variables.

// A static class.
public sealed class Utilities {
    // Private empty constructor to prevent instantiation.
    private Utilities() {
    }

    // One or more static members.
    public static void DoSomething() {
        ...
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s