SoFunction
Updated on 2025-03-06

Specific use of static keywords in C#

In C# programming, the static keyword is a very common feature that can be used for classes, methods, and member variables. This article will explain in detail the meaning, purpose, relationship with other keywords in C# and how it is used in different scopes. At the same time, we will also provide some examples to help you better understand the application of static keywords in C#.

Uses of static

  • Static members provide the functionality of global data sharing because they only have one instance in the entire application domain. This makes them ideal for sharing data across objects.
  • Static methods are often used to implement common functions because they do not require instantiation to be used. For example, many static methods are defined in the Math class, such as Abs, Sqrt, etc.
  • Static classes are usually used to implement a set of related functions without instantiation. For example, a class is a static class, which provides many ways to interact with the console, such as ReadLine, WriteLine, etc.
  • Static constructors are used to initialize static members, because these members are initialized only when they are accessed for the first time. Without a static constructor, we need to explicitly initialize static members in the code, which is usually tedious.

Static Properties

Static properties belong to a class, not to a specific instance of the class. This means you can access static properties without creating a class instance. Static properties are usually used to define some constant or tool properties that are the same as the application life cycle.

public class MyClass
{
    public static int MyStaticProperty
    {
        get { return 10; }
        set { }
    }
}

In this example, MyStaticProperty is a static property that can be accessed directly without instantiating the object. Each time MyStaticProperty is accessed, the constant value 10 is returned.

Static methods

A static method belongs to a class, not to a specific instance of the class. This means you can call static methods without creating a class instance. Static methods are often used to define some tool methods that are the same as the application life cycle.

public class MyClass
{
    public static void MyStaticMethod()
    {
        // Here you can define some tool methods    }
}

In this example, MyStaticMethod is a static method that can be called directly without instantiating the object. This method can perform some operations, such as calculating the sum of two numbers:

public class MyClass
{
    public static int Add(int a, int b)
    {
        return a + b;
    }
}

In this example, Add is a static method that takes two integer arguments and returns their sum. The Add method can be called directly without instantiating the MyClass object.

Static Variables

In a class, static variables can be defined using the static keyword. Static variables belong to a class, not to a specific instance of the class. This means it is shared by all instances of the class.

public class MyClass
{
    public static int MyStaticVariable = 10;
}

In this example, MyStaticVariable is a static variable that is shared by all MyClass instances. No matter how many MyClass instances are created, they will refer to the same MyStaticVariable variable.

Things to note

When using static keywords, you need to pay attention to the following points:

  • Static member variables and static member methods should be used with caution, as they may lead to resource leakage. Ensure that resources are released in a timely manner when they are not needed.
  • Static member methods cannot directly access non-static member variables and methods. If you need to access non-static members, you can create an instance:
public class MyClass
{
    public int MyNonStaticVariable;

    public static void MyStaticMethod(MyClass instance)
    {
         = 10;
    }
}

  • Static methods cannot be used as parameters to an instance method. If you need to pass an instance as a parameter, you can create a non-static method:
public class MyClass
{
    public void MyNonStaticMethod()
    {
        // Here you can define some tool methods    }
}
  • Static member variables and methods are accessed slower than non-static members because they need to be accessed by class names. Try to avoid using static members in performance-sensitive occasions.

Summarize

This article introduces in detail the meaning, purpose, relationship with other keywords in C# and its use in different scopes. Among them, static members provide the function of global data sharing, while static methods and static classes are often used to implement general functions. Static constructors are used to initialize static members, while static local variables can make the life of a variable longer. When using static keywords, you need to be careful to avoid overuse and understand how they are combined with other keywords.

This is the article about the specific use of static keywords in C#. For more related contents of C# static keywords, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!