C Sharp static

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

C# static

In C#, static is a keyword or modifier that belongs to the type not instance. So instance is not required to access the static members. In C#, static can be field, method, constructor, class, properties, operator and event.

Note: Indexers and destructors cannot be static.

Advantage of C# static keyword

Memory efficient: Now we don't need to create instance for accessing the static members, so it saves memory. Moreover, it belongs to the type, so it will not get memory each time when instance is created.

C# Static Field

A field which is declared as static, is called static field. Unlike instance field which gets memory each time whenever you create object, there is only one copy of static field created in the memory. It is shared to all the objects.

It is used to refer the common property of all objects such as rateOfInterest in case of Account, companyName in case of Employee etc.

C# static field example

Let's see the simple example of static field in C#.

 

  1. using System;  
  2.    public class Account  
  3.     {  
  4.         public int accno;   
  5.         public String name;  
  6.         public static float rateOfInterest=8.8f;  
  7.         public Account(int accno, String name)  
  8.         {  
  9.             this.accno = accno;  
  10.             this.name = name;  
  11.         }  
  12.           
  13.         public void display()  
  14.         {  
  15.             Console.WriteLine(accno + " " + name + " " + rateOfInterest);  
  16.         }  
  17.    }  
  18.    class TestAccount{  
  19.        public static void Main(string[] args)  
  20.         {  
  21.          Account a1 = new Account(101, "Sonoo");  
  22.             Account a2 = new Account(102, "Mahesh");  
  23.             a1.display();  
  24.             a2.display();  
  25.   
  26.         }  
  27.     }  

Output:

101 Sonoo 8.8
102 Mahesh 8.8

C# static field example 2: changing static field

If you change the value of static field, it will be applied to all the objects.

  1. using System;  
  2.    public class Account  
  3.     {  
  4.         public int accno;   
  5.         public String name;  
  6.         public static float rateOfInterest=8.8f;  
  7.         public Account(int accno, String name)  
  8.         {  
  9.             this.accno = accno;  
  10.             this.name = name;  
  11.         }  
  12.           
  13.         public void display()  
  14.         {  
  15.             Console.WriteLine(accno + " " + name + " " + rateOfInterest);  
  16.         }  
  17.    }  
  18.    class TestAccount{  
  19.        public static void Main(string[] args)  
  20.         {  
  21.             Account.rateOfInterest = 10.5f;//changing value  
  22.             Account a1 = new Account(101, "Sonoo");  
  23.             Account a2 = new Account(102, "Mahesh");  
  24.             a1.display();  
  25.             a2.display();  
  26.   
  27.         }  
  28.     }  

Output:

101 Sonoo 10.5
102 Mahesh 10.5

C# static field example 3: Counting Objects

Let's see another example of static keyword in C# which counts the objects.

  1. using System;  
  2.    public class Account  
  3.     {  
  4.         public int accno;   
  5.         public String name;  
  6.         public static int count=0;  
  7.         public Account(int accno, String name)  
  8.         {  
  9.             this.accno = accno;  
  10.             this.name = name;  
  11.             count++;  
  12.         }  
  13.           
  14.         public void display()  
  15.         {  
  16.             Console.WriteLine(accno + " " + name);  
  17.         }  
  18.    }  
  19.    class TestAccount{  
  20.        public static void Main(string[] args)  
  21.         {  
  22.             Account a1 = new Account(101, "Sonoo");  
  23.             Account a2 = new Account(102, "Mahesh");  
  24.             Account a3 = new Account(103, "Ajeet");  
  25.             a1.display();  
  26.             a2.display();  
  27.             a3.display();  
  28.             Console.WriteLine("Total Objects are: "+Account.count);  
  29.         }  
  30.     }  

Output:

101 Sonoo 
102 Mahesh 
103 Ajeet
Total Objects are: 3