Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am just curious to know that why c# 2.0 introduced static class ? I read somewhere that before static class everyone was using private constructor to do same work, it is true? if not than what was the actual reason?

Edit 1 : After posting this question I was again searing for answer and find out few good lines in this link - click here

Thanks

What I have tried:

Googled but could not able to find any answer for this.
Posted
Updated 26-Jun-16 4:24am
v2
Comments
BillWoodruff 26-Jun-16 10:59am    
The section on Static Classes in the book you link to has, imho, very poor examples, such as declaring a Struct without the 'Static modifier (or any other modifier), and then putting a 'Static 'ctor in the Struct. Downright bizarre.

Static classes were introduced to provide classes that can't be instantiated or inherited from. While you can create a Singleton class to prevent more than one instance existing, before V2.0 there was no way to enforce it. Static classes provide that enforcement by refusing to allow instance members or instance constructors - if you try, you will get a compilation error. Because you can't create an instance you can only access the methods via the className.memberName syntax, and you can't create a variable to reference a static class.
Later (at V3.0) they were used to hold Extension Methods, but these weren't available in V2.0

See MSDN for examples: Static Classes and Static Class Members (C# Programming Guide)[^]
 
Share this answer
 
Static class is a simple convenient method to enforce the absence of any non-static members. If you try to add any non-static member, and the class itself remains static, you get a error message. Also, inheritance is not allowed for the static class. So, it answers partially your question: this is, by far, not the same as preventing instantiation of a class, this is something else.

Also note that what you describe, adding a private parameteless (important!) constructor, is done just to remove the effect of having the default constructor). Naturally, you would not add any other constructors: adding a private one would make no sense, and adding any non-private one would allow instantiation.

Now, is this syntactic feature really needed for the functionality? Of course not. Everything would work without it. So what? Everything could work without properties, templates, access modifiers, using statement… and a lot more. You need to understand that many features in language are good for: fool-proof behavior of the compiler, better compiler diagnostics, convenience, maintenance, expressive capabilities, readability, and so on. See also: Syntactic sugar — Wikipedia, the free encyclopedia.

Please understand: your question of the type "why having this and that in the design?" maybe not really productive if it allows the answer in the "why not?" style. :-)

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900