Social Icons

Friday, 5 July 2013

Static Classes and Static Class members in C#.Net with Examples(Oops Programming)

Static Class and Static Class Members


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.


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.
Example for static class


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.


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:


Please Give your Valuable Suggestions,Questions and Comments to improve my blog.