C# static

HTML
CSS
C#
SQL

static Keyword in C#

When you declare a member (field, method, property, etc.) as static, it belongs to the class rather than an instance of the class.

This means there's only one copy of the static member shared by all instances.

public class MyClass

{

    // Static field

    public static int StaticField = 42;

    // Static method

    public static void StaticMethod()

    {

        Console.WriteLine(“Hello from a static method!”);

    }

}

Static Fields:

static fields are shared among all instances of the class.

Changes to a static field in one instance affect the value seen by other instances.

public class SharedData

{

    // Static field shared among all instances

    public static int SharedValue = 0;

 

    // Instance field

    public int InstanceValue = 0;

}

Example usage

SharedData instance1 = new SharedData();

SharedData instance2 = new SharedData();

 

instance1.SharedValue = 10;

Console.WriteLine(instance2.SharedValue); // Outputs 10

Static Methods:

static methods are often used for utility functions that don’t depend on instance-specific data.

public class MathUtility

{

    // Static method for adding two numbers

    public static int Add(int a, int b)

    {

        return a + b;

    }

}

Example usage

int result = MathUtility.Add(5, 7);

Static Constructors

static constructors are used to initialize static members of a class.

They are called once when the class is first accessed or before any static members are used.

public class InitializationExample

{

    // Static field

    public static int StaticField;

 

    // Static constructor

    static InitializationExample()

    {

        // Initialization code

        StaticField = 42;

    }

}

Singleton Pattern:

static is often used in the Singleton pattern, ensuring only one instance of a class is created.

public class Singleton

{

    // Private static instance

    private static Singleton instance;

    // Private constructor to prevent external instantiation

    private Singleton() { }

    // Public method to access the single instance

    public static Singleton Instance

    {

        get

        {

            if (instance == null)

            {

                instance = new Singleton();

            }

            return instance;

        }

    }

}

Example usage

Singleton obj1 = Singleton.Instance;

Singleton obj2 = Singleton.Instance;

 

Console.WriteLine(obj1 == obj2); // Outputs true

Static Classes:

A static class cannot be instantiated, and all members must be static.

It’s often used for utility classes with helper methods.

public static class UtilityClass

{

    // Static method in a static class

    public static void HelperMethod()

    {

        Console.WriteLine(“I’m a helper method in a static class!”);

    }

}

Example usage

UtilityClass.HelperMethod();

Use Cases:

  • To share a value among all instances of a class
  • Use static for elements that don’t rely on instance-specific data.
  • Consider the Singleton pattern when you need a single instance of a class.