C# Whidbey Featurette #3: Static classes


Because all functions in C# must live inside of a class, there are some clases – System.Math is a canonical example – that are merely collections of static methods. Since it’s useless to create an instance of such a class, in current versions of C#, you can protect against this by creating a private constructor. The constructor can never be called, and therefore no instance can be created.


There are three issues with this approach:



  1. The private constructor takes up space in the IL, metadata, etc. This isn’t a big deal, but it does have a tiny effect.

  2. It’s not clear from casual inspection of the source that this class only has static methods.

  3. There’s no protection against instance methods on static classes. The compiler will happily let you write methods that could never be called (in theory, a static could call a private constructor and return the instance, but not in this scenario)

  4. You could derive from the class if you forgot to mark it sealed

So, for Whidbey, we allow the user to mark a class as static, which means that it’s sealed, has no constructor, and the compiler will give you an error if you write an instance method.


Rumor has it that the 1.0 frameworks shipped with an instance method on a static class.


 

Comments (17)

  1. With reference to the fact that these classes are "sealed", I was recently investigating whether static methods were inherited:

    http://blogs.geekdojo.net/pdbartlett/archive/2004/04/06/1578.aspx

    I concluded they were, but if you check the comments you’ll see that a far more diligent fellow ‘dojo-er (Richard) actually checked out the IL which seemed to suggest that this is a C#-ism (syntactic sugar on the part of the C# compiler) rather than something supported by MSIL itself.

  2. Max says:

    While it’s nice to see the static keyword in Whidbey I was just wondering why you haven’t choosen "abstract sealed" instead?

  3. Wim says:

    "..the compiler will give you an error if you write an instance method."

    Just for completeness – I assume this isn’t limited to just instance methods, but also to instance properties and member fields, correct?

  4. Fernando Tubio says:

    So no more Environment.HasShutdownStarted fiascos… 🙂

  5. Talbott Crowell says:

    I like the idea of static classes and look forward to this improvement to C#. I don’t think "abstract sealed" is a good idea since abstract implies incomplete (requires inheritance for full implementation).

  6. Max says:

    @Talbott

    abstract does _not require_ inheritance for a full implementation:

    abstract class Foo

    {

    static void DoSomething(){}

    }



    Foo.DoSomething();

    is totally legal code. ‘abstract’ only means no instance can be created – at least at the view of the Compiler/Runtime.

    But I have to agree that most people think ‘abstract’ means an incomplete class. So it may be less confusing to introduce a "new" keyword instead.

  7. I suggest that Microsoft could extend the syntax of C# to allow checked and unchecked wherever unsafe is allowed.

  8. Anonymous says:

    Lazycoder weblog » new static modifier for classes in Whidbye

  9. enki says:

    Re: Max,

    It’s totally legal, but semantically, "abstract" sounds like something that shouldn’t exist on its own. Static gives a better definition of the actual function the keyword achieves.

  10. Talbott Crowell says:

    @Max

    Good point. I usually hide my constructors with a private constructor, but you could use abstract instead. Personally, I think semantics are key to keeping a language simple and the codebase built on it maintainable. Semantically, abstract implies abstraction, not utility. If keywords indicate intention, code is more maintainable because the code is self-documenting.

  11. Branco says:

    Ah, I see: just like VB.Net’s standard Modules… 😉

  12. href=//www.dmoz.net.cn/ wangzhidaquang

    href=//www.86dmoz.com/ jingpingwangzhi

    href=//www.kamun.com/ mianfeidianying

    href=//www.kamun.com/ dianyingxiazai

    href=//www.kamun.com/ MP3 free download

    href=//www.pc530.net/ diannaoaihaozhe

    href=//www.5icc.com/ duangxingcaixingxiazha

    href=//www.dianyingxiazai.com/ dianyingxiazai

    href=//www.yinyuexiazai.com/ yinyuexiazai