What Are Static Class And Static Class Members In C#

In this blog, I will discuss static classes and static class members in C#,

Static Classes and Static Class Members

A static class is a class which can not be instantiated. We do not need to create an object of a static class like we did in non-static classes. Members of static classes can be accessed directly using the class name followed by a (.) and class member name. Class Members can be methods, fields, properties, or events. A static class can contain only the static members while a non-static class can contain static members. Also, Static classes are by-default sealed so they cannot be inherited.

To declare a class and its members as static, 'static' keyword are used as shown below,
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     // Static class  
  6.     static class TutLovers  
  7.     {  
  8.         // Static Variable  
  9.         public static int val_Static;  
  10.   
  11.         // Static Method  
  12.         public static void Addition_Static()  
  13.         {  
  14.             Console.Write("Static Method");  
  15.         }  
  16.   
  17.         // Static property  
  18.         public static int Addition_NonStatic  
  19.         {  
  20.             getset;  
  21.         }  
  22.     }  
For more understanding see the example shown below,

The example shown below contains two non-static classes named "TutLovers" and "TutHaters". Here a class "TutLovers" contains static as well as non-static methods and variable. When we access them from class "TutHaters" then, For the static members, they will be accessed directly using class name followed by (.) and
For non-static members, we have created an object of that class.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class TutLovers  
  6.     {  
  7.         public static int val_Static;  
  8.   
  9.         public int val_NonStatic;  
  10.   
  11.         public static void Addition_Static()  
  12.         {  
  13.             Console.Write("Static Method");  
  14.         }  
  15.   
  16.         public void Addition_NonStatic()  
  17.         {  
  18.             Console.Write("Non-Static Method");  
  19.         }  
  20.     }  
  21.   
  22.     //If the class is non-static  
  23.     class TutHaters  
  24.     {  
  25.         public void Haters()  
  26.         {  
  27.             //Object of TutLovers class is created  
  28.             TutLovers tutLovers = new TutLovers();  
  29.   
  30.             //Non-Static members can be called only from object  
  31.             tutLovers.Addition_NonStatic();  
  32.             tutLovers.val_NonStatic = 5;  
  33.   
  34.             //Static members can only be called using class_name followed by a (.)  
  35.             TutLovers.Addition_Static();  
  36.             TutLovers.val_Static = 5;  
  37.   
  38.             /* When we try to access static member from object then it will create an error  
  39.             Error as "Members 'TutLovers.Addition_Static()' can not be accessed with an instance referen            -ce; qualify it with a type name instead" */
  40.             tutLovers.Addition_Static();  
  41.   
  42.             /* When we try to access non-static member using class_name then it will create an error  
  43.             Error as "An object reference is required for the non-static field                                      , method or property 'TutLovers.Addition_NonStatic()'" */
  44.             TutLovers.Addition_NonStatic();  
  45.         }  
  46.     }  
  47.   
  48. }  
As shown above, when we try to access static member with an instance reference, it will create an error as "Members 'TutLovers.Addition_Static()' can not be accessed with an instance reference; qualify it with a type name instead" and when we try to access non-static member using class_name then it will create an error as "An object reference is required for the non-static field, method or property 'TutLovers.Addition_NonStatic()'".

Note
  1. There exists only one copy for every static member regardless of the number of instances of the class.
  2. Static methods can access only static members. They cannot access non-static members.
  3. A static class can only contain static members.
Ebook Download
View all
Learn
View all