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:
- The private constructor takes up space in the IL, metadata, etc. This isn't a big deal, but it does have a tiny effect.
- It's not clear from casual inspection of the source that this class only has static methods.
- 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)
- 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
- Anonymous
April 13, 2004
The comment has been removed - Anonymous
April 13, 2004
While it's nice to see the static keyword in Whidbey I was just wondering why you haven't choosen "abstract sealed" instead? - Anonymous
April 13, 2004
"..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? - Anonymous
April 13, 2004
So no more Environment.HasShutdownStarted fiascos... :) - Anonymous
April 13, 2004
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). - Anonymous
April 14, 2004
The comment has been removed - Anonymous
April 14, 2004
I suggest that Microsoft could extend the syntax of C# to allow checked and unchecked wherever unsafe is allowed. - Anonymous
April 14, 2004
Lazycoder weblog » new static modifier for classes in Whidbye - Anonymous
April 15, 2004
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. - Anonymous
May 04, 2004
@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. - Anonymous
May 22, 2004
Ah, I see: just like VB.Net's standard Modules... ;-) - Anonymous
August 01, 2004
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