C Sharp static keyword

C# static

Instead of the instance, the static keyword or modifier in C# belongs to the type. Thus the static members do not need an instance for its access. The static in C# can be field, method, constructor, class, properties, operator or event, but the indexers and destructors cannot be static.

Advantages:

The C# static keyword is memory efficient as it doesn’t need to create an instance for its access. It will also not need memory whenever the instance is created, because it belongs to the type.

C# Static Field:

A static field is the one that is declared as static. Only one copy of the static field created in the memory is shared with all the objects. However, the instance field which gets memory each time whenever an object is created. The common property of all the objects is usually referred to using the C# static field.

Example 1:

using System;  
   public class Loan  
    {  
        public int id;  
        public String type;  
        public static float rateOfInterest=2.5f;  
        public Loan(int id, String type)  
        {  
            this.id = id; 
            this.type = type;  
        }  
 
        public void display()  
        {  
            Console.WriteLine(id + " " + type + " " + rateOfInterest);  
        }  
   }  
   class Details{  
       public static void Main(string[] args)  
        {  
         Loan b1 = new Loan(10, "Home");  
            Loan b2 = new Loan(20, "Car");  
            b1.display();  
            b2.display();  
 
        }  
    }

Output:

Explanation:

In the above example, we are displaying the use of the static field in C#. The value of the objects are thus displayed on the screen after the execution of the above code.

Example 2:

using System;  
   public class Loan  
    {  
        public int id;   
        public String type;  
        public static float rateOfInterest=2.5f;  
        public Loan(int id, String type)  
        {  
            this.id = id;  
            this.type = type;  
        }  
 
        public void display()  
        {  
            Console.WriteLine(id + " " + type + " " + rateOfInterest);  
        }  
   }  
   class Details{  
       public static void Main(string[] args)  
        {  
            Loan.rateOfInterest = 20.5f;//changing value  
            Loan b1 = new Loan(10, "Home");  
            Loan b2 = new Loan(20, "Car");  
            b1.display();  
            b2.display();  
 
        }  
    }

Output:

Explanation:

In the above example, we are changing the value of the static field, which will be applied to all the objects. The value of the objects are thus displayed on the screen after the execution of the above code.

Example 3:

using System;  
   public class Loan  
    {  
        public int id;   
        public String type;  
        public static int count=0;  
        public Loan(int id, String type)  
        {  
            this.id = id;  
            this.type = type;  
            count++;  
        }  
 
        public void display()  
        {  
            Console.WriteLine(id + " " + type);  
        }  
   }  
   class Details{  
       public static void Main(string[] args)  
        {  
            Loan b1 = new Loan(10, "Home");  
            Loan b2 = new Loan(20, "Car");  
            Loan b3 = new Loan(30, "Personal");  
            b1.display();  
            b2.display();  
            b3.display();  
            Console.WriteLine("Count is: " + Loan.count);  
        }  
    }

Output:

Explanation:

In the above example, we are using the static keyword in C# to count the objects. Along with the count of the objects, we are also displaying the value of the objects.

Please follow and like us:
Content Protection by DMCA.com