Section 1.8. Create Static Classes


1.8. Create Static Classes

In addition to declaring methods as being static, now you also can declare classes as being static.

The purpose of a static class is to provide a set of static utility methods scoped to the name of the class, much as you see done with the Convert class in the Framework Class Library.


Note: In C# 2.0 you can declare an entire class as being static to signal that you've scoped a set of static utility methods to that class.

1.8.1. How do I do that?

To create a static class, just add the static keyword before the class name and make sure your static class meets the criteria described earlier for static members. Also, note that static classes have the following restrictions:

  • They can contain only static members.

  • It is not legal to instantiate a static class.

  • All static classes are sealed (you cannot derive them from a static class).

In addition to these restrictions, a static class cannot contain a constructor. Example 1-6 shows the proper use of a static class.

Example 1-6. Using static classes
#region Using directives     using System;     #endregion     namespace StaticClass {        public static class CupConversions    {       public static int CupToOz(int cups)       {          return cups * 8; // 8 ounces in a cup       }       public static double CupToPint(double cups)       {          return cups * 0.5;  // 1 cup = 1/2 pint       }           public static double CupToMil(double cups)       {          return cups * 237; // 237 mil to 1 cup       }           public static double CupToPeck(double cups)       {          return cups / 32; // 8 quarts = 1 peck       }           public static double CupToBushel(double cups)       {          return cups / 128; // 4 pecks = 1 bushel       }    }        class Program    {       static void Main(string[  ] args)       {          Console.WriteLine("You might like to know that " +              "1 cup liquid measure is equal to: ");          Console.WriteLine(CupConversions.CupToOz(1) + " ounces");          Console.WriteLine(CupConversions.CupToPint(1) + " pints");          Console.WriteLine(CupConversions.CupToMil(1) + " milliliters");          Console.WriteLine(CupConversions.CupToPeck(1) + " pecks");          Console.WriteLine(CupConversions.CupToBushel(1) + " bushels");       }    } }

Output:

You might like to know that 1 cup liquid measure is equal to:  8 ounces 0.5 pints 237 milliliters 0.03125 pecks 0.0078125 bushels

The Program class's main method makes calls on the static methods of the CupConversions class. Because CupConversions exists only to provide several helper methods, and no instance of CupConversions is ever needed, it is safe and clean to make CupConversions a static class.

1.8.2. What about . . .

...fields and properties? Can my static class have such members?

Yes, they can, but all the members (methods, fields, and properties) must be static.

1.8.3. Where can I learn more?

Eric Gunnerson has written an excellent article on static classes. You can find it in MSDN at http://blogs.msdn.com/ericgu/archive/2004/04/13/112274.aspx.



Visual C# 2005(c) A Developer's Notebook
Visual C# 2005: A Developers Notebook
ISBN: 059600799X
EAN: 2147483647
Year: 2006
Pages: 95
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net