Welcome to MSDN Blogs Sign in | Join | Help

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.

 

Published Tuesday, April 13, 2004 8:06 AM by ericgu
Filed under: ,

Comments

Tuesday, April 13, 2004 8:45 AM by Paul Bartlett

# re: C# Whidbey Featurette #3: Static classes

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.
Tuesday, April 13, 2004 11:25 AM by Max

# re: C# Whidbey Featurette #3: Static classes

While it's nice to see the static keyword in Whidbey I was just wondering why you haven't choosen "abstract sealed" instead?
Tuesday, April 13, 2004 12:34 PM by Wim

# re: C# Whidbey Featurette #3: Static classes

"..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?
Tuesday, April 13, 2004 7:00 PM by Fernando Tubio

# re: C# Whidbey Featurette #3: Static classes

So no more Environment.HasShutdownStarted fiascos... :)
Tuesday, April 13, 2004 10:18 PM by Talbott Crowell

# re: C# Whidbey Featurette #3: Static classes

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).
Wednesday, April 14, 2004 10:55 AM by Something Clever

# C# Featurette Suggestion

I suggest that Microsoft could extend the syntax of C# to allow checked and unchecked wherever unsafe is allowed.
Wednesday, April 14, 2004 9:13 AM by Max

# re: C# Whidbey Featurette #3: Static classes

@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.
Wednesday, April 14, 2004 1:46 PM by TrackBack

# Lazycoder weblog » new static modifier for classes in Whidbye

Lazycoder weblog » new static modifier for classes in Whidbye
Wednesday, April 14, 2004 3:05 PM by Code/Tea/Etc...

# C# Featurette #3 from Eric Gunnerson

Wednesday, April 14, 2004 3:59 PM by Christian Nagel's OneNotes

# Whidbey C# Featurette

Thursday, April 15, 2004 8:31 AM by Sam Gentile's Blog

# New and Notable 45

Thursday, April 15, 2004 10:12 AM by enki

# re: C# Whidbey Featurette #3: Static classes

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.
Monday, April 19, 2004 12:20 AM by Information -

# Static Class - Abstract does not require inheritence for full implementation.

Tuesday, May 04, 2004 6:02 PM by Talbott Crowell

# re: C# Whidbey Featurette #3: Static classes

@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.
Saturday, May 22, 2004 7:37 PM by Branco

# re: C# Whidbey Featurette #3: Static classes

Ah, I see: just like VB.Net's standard Modules... ;-)
Sunday, August 01, 2004 11:34 PM by dianyingxiazai

# re: C# Whidbey Featurette #3: Static classes

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
Friday, January 14, 2005 2:57 AM by Christian Nagel's OneNotes

# Whidbey C# Featurette

New Comments to this post are disabled
 
Page view tracker