C# usage of Static Keyword

//static field
using System;

namespace StaticKeyword
{
    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 Program
    {
        static void Main(string[] args)
        {

            Account a1=new Account(101,"Soonoo");
            Account a2=new Account(102, "Mahesh");
            a1.display();
            a2.display();
            Console.ReadKey();
        }
    }
}
//changing static field
using System;

namespace StaticKeyword
{

    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 Program
    {
        static void Main(string[] args)
        {

            Account.rateOfInterest = 55.65f; //we are changing the value of this variable by calling from main method
            Account a1=new Account(101,"Soonoo");
            Account a2=new Account(102, "Mahesh");
            a1.display();
            a2.display();
            Console.ReadKey();
        }
    }
}
//changing static field when calling from another field
//without static declaring it is not possible
using System;

namespace StaticKeyword
{

    public class Account
    {
        public int accno;
        public string name;
        public static int count = 0;
        public static float rateOfInterest = 8.8f;

        public Account(int accno,string name)
        {
            this.accno = accno;
            this.name = name;
            count++;
        }

        public void display()
        {
            Console.WriteLine(accno+" "+name+" "+rateOfInterest);
        }

    }

    class Program
    {
        static void Main(string[] args)
        {

            Account.rateOfInterest = 55.65f; //we are changing the value of this variable by calling from main method
            Account a1=new Account(101,"Soonoo");
            Account a2=new Account(102, "Mahesh");
            Account a3 = new Account(102, "Jatra");
            Account a4 = new Account(102, "Jatra pala");
            a1.display();
            a2.display();
            a3.display();
            Console.WriteLine("Total object count here:"+Account.count);
            Console.ReadKey();
        }
    }
}

Static Class Example:

//using static class
//we need object reference where it is clearing up
//it is sealed and cannot be called/instantiated

using System;

namespace StaticKeyword
{
    public static class MyMath
    {

        public static float pi = 3.14f;

        public static int cube(int n)
        {
            return n * n * n;
        }
    }


    class TestMath
    {
        public static void Main(string[] args)
        {
            
            Console.WriteLine("Value of PI is:"+MyMath.pi);
            Console.WriteLine("Cube of 3 is:"+MyMath.cube(3));
            Console.ReadKey();
        }
        
    }
}

Static Constructor:

using System;


public class Account
{
    public int id;
    public string name;
    public static float rateOfInterest;

    public Account(int id, string name)
    {
        this.id = id;
        this.name = name;
    }

    static Account()
    {
        rateOfInterest = 9.5f;
    }

    public void display()
    {
        Console.WriteLine(id+" "+name+" "+rateOfInterest );
    }

}

class TestEmployee
{
    public static void Main(string[] args)
    {
        Account a1=new Account(101,"dss");
        Account a2=new Account(202,"dssd");
        a1.display();
        a2.display();
        Console.ReadKey();
    }
}

 

 

 

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *