find and solve || findandsolve.com
Please wait.....

C# Static Class

In C#, static means something which cannot be instantiated. We cannot create an object of a static class and cannot access static members.

Advantage of C# static keyword

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

C# Static Field

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

Example

   using System;  
   public class EmployeeInfo
    {
        public int ID;
        public string Name;
        public EmployeeInfo(int id, string name)
        {
            this.ID = id;
            this.Name = name;
        }
        public void display()
        {
            Console.WriteLine(ID + " " + Name);
        }
   }
    public class TestAccount{
       public static void Main(string[] args)         {            EmployeeInfo a1 = new EmployeeInfo(1001, "Sylvia");
            EmployeeInfo a2 = new EmployeeInfo(1002, "Rahul");
            a1.display();
            a2.display();
        }
    }  

Output

1001 Sylvia

1002 Rahul

C# static field : changing static field

   using System; 
   public class EmployeeInfo
    {         public int accno;
        public String name;
        public static float rateOfInterest=8.8f;
        public EmployeeInfo(int accno, String name)
        {
            this.accno = accno;
            this.name = name;
        }
        public void display()
        {
            Console.WriteLine(accno + " " + name + " " + rateOfInterest);
        }
   }
   public class AnotherClassEmployeeInfo {
       public static void Main(string[] args)
        {
            EmployeeInfo.rateOfInterest = 10.5f;//changing value
            EmployeeInfo obj = new EmployeeInfo(1001, "Sylvia");
            EmployeeInfo obj1 = new EmployeeInfo(1002, "Rahul");
            obj.display();
            obj1.display();
        }
    } 

Output

1001 Sylvia 10.5

1002 Rahul 10.5

C# static field : Counting Objects

   using System;  
   public class EmployeeInfo     {         public int AccountNo;
        public string Name;
        public static int count=0;
        public EmployeeInfo(int accno, String name)
        {
            this.AccountNo = accno;
            this.Name = name;
            count++;
        }
        public void display()
        {
            Console.WriteLine(AccountNo + " " + Name);
        }
   }
   public class AnotherClassEmployeeInfo{
       public static void Main(string[] args)
        {
            EmployeeInfo obj = new EmployeeInfo(1001, "Sylvia");
            EmployeeInfo obj1 = new EmployeeInfo(1002, "Rahul");
            EmployeeInfo obj2 = new EmployeeInfo(1003, "Debin");
            obj.display();
            obj1.display();
            obj2.display();
            Console.WriteLine("Total Objects are: "+EmployeeInfo.count);
        }
    }  

Output

1001 Sylvia

1002 Rahul

1003 Debin

Total Objects are: 3