Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi All,

Why do we have static class in .Net framework?
As if we dont create object of staic classes? So how do we say that staic class is following under opps conceps? As OOP says Object Oriented programming?

PS:-
I do understand that using staic class have following advantages
1) Compiler ensures that no instance are incidently created.
2) Member of Static class can be called directly no need to create any object.
3) Static class contain static members
4) The namespace or program in which we have static class when that got loaded static class also got loaded.

Please help me.

Thanks in Advance.
Posted
Updated 8-Jun-11 20:12pm
v3

One of the advantages of having static class is to provide its methods accessible application wide.
E.g. ConfigurationManager class in System.configuration dll allows us to read App.Config file across application. See MSDN info for it. http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx[^]

In general static class should contain information required across application such as applying certain application formatting. Such application wide functionalities can be defined by creating utility classes. (like ConfigurationManager - utility to access config file.)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Jun-11 3:25am    
Oh, so many down-votes today... I voted this answer 5, but I have some notes.
Static class is not a very good way of making application-wide access to the members. First, it's much better to implement a real singleton pattern, which is a non-static class with the only one static instance of it (it one does it right). Another approach: create a stack variable in the entry-point method (Main) and pass if further; finally, it could be a member field of some class which is already used as a singleton (or nearly), such as main window or form.

Please see my answer where I cover technical and supportability aspects.
--SA
--SA
sushil_gupta 9-Jun-11 6:29am    
hey...SAKryukov...Are you member of CodeProject technical team? Or by your wish you asnwer all the question we ask? does they pay you something? What kind of experience is required to become member like you?
A static class is a way to make a class which has nothing in common with OOP. Essentially, this is a package of members which are completely equivalent to a set of static variables, constants and methods (well properties are added) as they are understood before adventure of OOP. This effect can be obtained by having all members of the class static. Another extra feature is added access modifiers. Making class static by using key word static makes no functional difference at all.

So, why using key word static for a class?

This is just a feature of support of programming activity, a device used to protect us from accidental programming mistakes. If we do it, it prevents us from:

  • Accidentally adding an instance member to the class;
  • Accidental instantiation of the class when creating of the instance would not make sense.
  • Using a class as a generic type parameter with the type constraint new.


When we try to do either of these mistakes, the code will not compile.

Pure static classes (that is, all classes with the key word static) has more or less limited application. At the same time, they are very useful in certain cases. A typical use of such class is a utility class, a collection of some utility methods working only through explicit parameters. Some method are done private, other methods can call them or each other, but functionally all methods are independent.

—SA
 
Share this answer
 
v3
Comments
Toniyo Jackson 9-Jun-11 2:21am    
Excellent. My +5
Sergey Alexandrovich Kryukov 9-Jun-11 3:08am    
Thank you very much, Toniyo.
--SA

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