Static Class and Static Class Members
Introduction:
In my previous articles we saw Oops Programming Concepts like What are Access Modifiers in C#.Net,Abstract Methods and Abstract classes in C#.Net and Various artilces on WPF and Asp.Net.
Now we will see what is static class in C#.Net, How to access static class members,Is static class can be inherited?, can we create object of the static class using new keyword?,How static classes are identical to sealed classes and How Static classes are different to Abstract classes etc.
Description:
Static Class:
A class which declared using Static modifier is called as Static class.Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members are independent of any object identity.
A static class can only contains static members. we can not create object of the static class using new keyword. You can access the members of class using name of the class itself.Static classes are loaded automatically by .Net Framework CLR when the program or namespace containing the class is loaded.
The Static Classes have following features
- They only contains static members.
- They can not be instantiated.
- They are sealed so that can not be inherited.
- They can not contain instance constructors.
Static Fields:
As Shown in Above example Static Fields are created Using Static Modifier before datatype of field.
All un-initialized static fields get automatically initialized to their default values when the containing class is loaded for the first time.These Fields can be accessed by using class name.
Static Methods:
As Shown in Above example Static methods can be declared using static keyword before the return type of the method. These methods are accessed directly from the class. Static methods are faster than instance methods.
Comparison with Sealed Class and Abstract classes:
There is similarity between sealed class and static class i.e both can not be inherited. The only difference is static classes are accessed using class name directly and Sealed class members are accessed using object of the class. A sealed class contains both static and non - static members where as static class contains only static members.
When compare to abstract classes, abstract classes are inherited but static classes can not be inherited.Abstract classes contains only method definition not any body where as static classes contains full method definition and body.
Interview Questions:
Similarities between Static Class and Sealed class, differences between Abstract class and Static Class.
Conclusion:
I hope you got full pledge information about static classes and Static members in C#.Net. I hope you will comment your opinion about this.
Related Articles:
- Architecture of WPF
- Abstract Classes and Methods in C# .
- what is the Use of Sealed Class in C# with Example.
- Asp.Net Validation Controls Tutorial
- Asp.Net Ajax Tutorials
- .How to read and write key values from and to App.config Settings file .Net Windows Application.
- How to Use RangeValidator Control in Asp.Net with Example.
- Asp.Net Ajax Server Controls, Ajax Control Toolkit Download and Installation Tutorial.
- How to Use ViewState in Asp.Net(State Management Techniques).
- How to use Hidden Field in Asp.Net with Example(State Managemet Techniques).
- Beginners Tutorial on How to use QueryStrings in Asp.Net(State Management Techniques).
- Asp.Net Cookie Usage Overview with example(State Management Techniques).
- Access Specifiers or Modifiers in C# with example(Oops Concept).
When we use real time scenario's static classes?
ReplyDeletesuper.....shekar..
ReplyDeletesuper explanation shekar ..simple and clear..
ReplyDeleteGood explanation sekhar...:-)
ReplyDelete