Static Class And Static Class Members In C#

Today, I will explain the concept of Static class and Static class Members in C# with some examples and how we can use it in a Static class .

Introduction

 
A static class is created using the "Static" keyword in C#. Static classes cannot be instantiated or inherited but they can be accessed by static members only (static method, static variable, static constructor and etc..) but cannot be accessible to the non-static data members. We cannot create an instance for a static class.
 

Flow Chart

 
Static Class And Static Class Members In C#
Fig: 1
 

How to create a static class

  1. Create a class Name Details (or) keep as it as your wish.
  2. Create the Static class by using “Static” Keyword.
  3. Write the following code to create a static class
Syntax
  1. static class Details  
  2.      {  
  3.      }  

How to create a Static Method

  1. Create the Static Method by using “Static” Keyword.
  2. Create a Method Name Main (or) keep as it as your wish.
  3. Write the following code to create a static Method
Syntax
  1. public static class Details  
  2.     {  
  3.        //Static Method  
  4.        public static void Main(string FirstName,string LastName,string City)  
  5.         {  
  6.         }  
  7.     }  

How to create a Static Variable

 
Static Variable
  1. Static variables can be initialized outside the member function or class definition.
  2. Static variables are used to define constants because their values can be retrieved by invoking the class without creating an instance.
Syntax
  1. public static class Details  
  2.    {  
  3.         //Static Variables  
  4.         public static int Sno;  
  5.         public static string FirstName;  
  6.         public static string LastName;  
  7.         public static string City;  
  8.   }  

How to create a Static Constructor

 
Static Constructor
  1. Static constructor does not take access modifiers.
  2. A static constructor cannot be called directly and is only meant to be called by the common language runtime (CLR).It is invoked automatically
Syntax
  1. public static class Details  
  2.      {  
  3.        public static int Sno;  
  4.        public static string FirstName;  
  5.        public static string LastName;  
  6.        public static string City;  
  7.        //Static Constructor  
  8.        static Details()  
  9.        {  
  10.            Sno = 1;  
  11.            FirstName = "ABCD";  
  12.            LastName = "A";  
  13.            City = "World";           
  14.        }  
  15.      }  

Example

  1. Create a class Name Common (or) keep as it as your wish.
  2. Write the following code to create a static Class.
Coding
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Static_Class  
  8.  {  
  9.      public static class Common  
  10.      {  
  11.                 public static string Details(string FirstName,string LastName,string City)  
  12.         {  
  13.   
  14.             return (FirstName + " " + LastName);  
  15.         }          
  16.     }    
  17.  }  
  1. To create a method, call the Static class named Common and implement the method.
  2. Write the following code
Coding
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Windows;  
  7. using System.Windows.Controls;  
  8. using System.Windows.Data;  
  9. using System.Windows.Documents;  
  10. using System.Windows.Input;  
  11. using System.Windows.Media;  
  12. using System.Windows.Media.Imaging;  
  13. using System.Windows.Navigation;  
  14. using System.Windows.Shapes;  
  15.   
  16. namespace Static_Class  
  17. {  
  18.     /// <summary>  
  19.     /// Interaction logic for MainWindow.xaml  
  20.     /// </summary>  
  21.     public partial class MainWindow : Window  
  22.     {  
  23.         public MainWindow()  
  24.         {  
  25.             InitializeComponent();  
  26.         }  
  27.         public  void GetDetails()  
  28.         {  
  29.             var details=  Common.Details(txtFirstName.Text, txtLastName.Text, txtCity.Text);  
  30.             MessageBox.Show("Welcome to"+" "+details, "Details");  
  31.         }  
  32.           
  33.         private void BtnInsert_Click(object sender, RoutedEventArgs e)  
  34.         {  
  35.             GetDetails();  
  36.         }  
  37.     }  
  38. }  

Output

 
Static Class And Static Class Members In C# 

Summary

 
In this article, I discussed the Static class and Static Members in C#. I have written this article focusing on beginners and students.