Understanding Static vs Non-Static Variables in C#

Understanding Static variables

Whenever a variable has a static keyword or is declared inside the static block, the variable is considered static.

Here is an example.

namespace BasicCsharp
{   
    class className
    {
        int numb = 1;// non-static variable
        static int num2 = 2; // static variable
        public static void Main(string[] args) 
        {
            int a = 1;//static variable by default
        }
        public void randomMethod()
        {
            int b = 2;//non static viariable by default
        }
    }
}

You can make the variable static when you want that variable to have the same values through all instances that will be created.

Static vs Non-Static variables

Static variables are initialized when once the execution of the class starts, on the other side non-static variables initialize after the object is created.

Non-static variables get initialized n times depending on how many times the object is created, but static variables only initiate once during the lifecycle.

You can call the static variable of class outside of class but for non-static you need to instance to call the variable.

namespace BasicCsharp
{
    class className
    {
        int nonStatic; // non static variable
        static int yesStatic = 200;
        public className(int nonStatic)
        {
            this.nonStatic = nonStatic;
        }
        static void Main(string[] args)
        {
            //before instantiating className
            Console.WriteLine($"static value of yesStatic {className.yesStatic}");
            //within in the same class you can access without className
            Console.WriteLine($"static value of yesStatic within sameclass {yesStatic}");
            //instantiating for nonStatic
            className obj1 = new className(100);
            Console.WriteLine($"non static value of nonStatic {obj1.nonStatic}");
        }
    }
}

This example shows how static variables can be called and how nonstatic variables can be called.

The outcome of this code is

static value of yesstatic 200
static value of yesStatic within the class 200
non-static value of nonStatic 100

Scope of Static and Non-Static

For the static variable, the scope is Application scope which means it is created when class execution starts and it lasts as long as the application last.

Non-static variable is created when the object is created and also gets deleted when the object is deleted.

Also when you want to use a non-static variable in the static method you need to instantiate first to use a non-static variable, without instance you cannot use a non-static variable in a static method

Static and Non-Static constructor

If we make a static constructor we need to call it on top, before any other constructor and there only can be one static constructor in a class. also static constructor is only called once in the application's lifetime

Meanwhile non-static constructor is called on every object created.

namespace BasicCsharp
{

    class className
    {
        static className() 
        {
            Console.WriteLine("Static constructor is being called");
        }
        public className()
        {
            Console.WriteLine("Non-static constructor is being called");
        }

        static void Main(string[] args)
        {
            className obj1 = new className();
            className obj2 = new className();
        }
    }
}

The outcome of this code is

Static constructor is being called
Non-static constructor is being called
Non-static constructor is being called

As you can see above, the static constructor is only called once while non static constructor is called twice on creating two objects.

Static in class

class with a static modifier, it can only have static variables and methods. and you cannot create an object with a static class and we can access what's in a static class by using the class name.

namespace BasicCsharp
{

    public static class math
    {
        public static int add(int a, int b){  return a + b; }
        public static int subtract(int a, int b) {  return a - b; }
        public static float multiply(float a, float b) {  return a * b; }
        public static double divide(double a, double b) {  return a / b; }
    }

    class className
    {

        static void Main(string[] args)
        {
            int addition = math.add(3, 3);
            int subtraction = math.subtract(3, 3);
            float multiplication = math.multiply(3, 3);
            double divisiaon = math.divide(3, 3);

            Console.WriteLine($"addition = {addition}, subtraction = {subtraction}, multiplication = {multiplication}, division = {divisiaon}");
        }
    }
}

This is an example of a static class, like you can see it does not need to be instantiated to be used. it can use the methods in the class by going through static class.

The outcome is

addition = 6, subtraction = 0, multiplication = 9, division = 1