C# Static Variable | C# static - c# - c# tutorial - c# net




C# Static Variable

Learn c# - c# tutorial - C# Static Variable - c# examples - c# programs

What is the use of static variable in C# ?

  • 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.
 Static Variable

Static Variable

  • 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.
Related Tags: kurs c# , c# programmieren , tutorial c# visual studio , learn programming with c# , c# kurs online , the best way to learn c# , c# tutorial for complete beginners from scratch , tuto c# , manual c#

C# static field example

  • Let's see the simple example of static field in C#.
using System;  
   public class Account  
    {  
        public int accno;   
        public String name;  
        public static float rateOfInterest=8.8f;  
        public Account(int accno, String name)  
        {  
            this.accno = accno;  
            this.name = name;  
        }  
          
        public void display()  
        {  
            Console.WriteLine(accno + " " + name + " " + rateOfInterest);  
        }  
   }  
   class TestAccount{  
       public static void Main(string[] args)  
        {  
        Account a1 = new Account(101, "Sonoo");  
            Account a2 = new Account(102, "Mahesh");  
            a1.display();  
            a2.display();  
  
        }  
    }  
click below button to copy the code. By - c# tutorial - team

C# examples - 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.
using System;  
   public class Account  
    {  
        public int accno;   
        public String name;  
        public static float rateOfInterest=8.8f;  
        public Account(int accno, String name)  
        {  
            this.accno = accno;  
            this.name = name;  
        }  
          
        public void display()  
        {  
            Console.WriteLine(accno + " " + name + " " + rateOfInterest);  
        }  
   }  
   class TestAccount{  
       public static void Main(string[] args)  
        {  
           Account.rateOfInterest = 10.5f;//changing value  
            Account a1 = new Account(101, "Sonoo");  
            Account a2 = new Account(102, "Mahesh");  
            a1.display();  
            a2.display();  
  
        }  
    }  


click below button to copy the code. By - c# tutorial - team

C# examples - 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.
using System;  
   public class Account  
    {  
        public int accno;   
        public String name;  
        public static int count=0;  
        public Account(int accno, String name)  
        {  
            this.accno = accno;  
            this.name = name;  
           count++;  
        }  
          
        public void display()  
      {  
            Console.WriteLine(accno + " " + name);  
        }  
   }  
   class TestAccount{  
       public static void Main(string[] args)  
        {  
            Account a1 = new Account(101, "Sonoo");  
            Account a2 = new Account(102, "Mahesh");  
            Account a3 = new Account(103, "Ajeet");  
            a1.display();  
            a2.display();  
            a3.display();  
            Console.WriteLine("Total Objects are: "+Account.count);  
        }  
    }  
click below button to copy the code. By - c# tutorial - team
Related Tags: kurs c# , c# programmieren , tutorial c# visual studio , learn programming with c# , c# kurs online , the best way to learn c# , c# tutorial for complete beginners from scratch , tuto c# , manual c#

C# examples - Output :

101 Sonoo 
102 Mahesh 
103 Ajeet
Total Objects are: 3


Related Searches to c# Static Variable | C# static

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×