static classes

  • Thread starter Thread starter Willem van Rumpt
  • Start date Start date
W

Willem van Rumpt

Hi all,
I was wondering: Why isn't it possible to write "static classes". By
this I mean a class that will _never_ get instantiated, but will always
work on and with other static members of its class.

The class would in effect behave like a "classic" win32 dll and its
exported functions, only this time referred by
<classname>.<ExportedFunction>

I know (how) I can achieve the same results with a singleton, but why
not just mark the entire class as "static" instead of marking every
single member as static?

I'm quite new to .NET (digging, gropin and mutilating around for about 8
months now), so, my questions are:
- Is it possible?
- If not, are there better solutions to my "problem"?
- Would it any how be a nice language/platform addition?

Thanks in advance,

Willem
 
Willem van Rumpt said:
Hi all,
I was wondering: Why isn't it possible to write "static classes". By this
I mean a class that will _never_ get instantiated, but will always work on
and with other static members of its class.

Probably because no one thought it would be as valueable as it has turned
out to be. C# 2.0 will support static classes for pretty much this reason.
You will have to mark every member as static still, but the compiler throws
an error if you don't, and it takes care of the sealing and private
constructor.

Outside of that, what you want is achievable, just mark your class as sealed
and give it a private constructor(with no public ones). That'll stop
initalizations.
 
Willem said:
Hi all,
I was wondering: Why isn't it possible to write "static classes". By
this I mean a class that will _never_ get instantiated, but will always
work on and with other static members of its class.

Excellent idea and as other posters have pointed out -- will be implemented
in 2.0

As far as why it wasn't implemented before, I think the bent was towards
classes as universals that are shared between applications -- rather than
called within a single app ( which, in practice, is actually far more
common ).
 
thank you all for your replies. I'll make do with the provided
solutions, have a look at the C# 2.0 specs, and wait for Whidbey to
arrive (somewhere late autumn 2008, as I've understood ;) ).

Willem van Rumpt
 
Back
Top