c#static (static) keyword

In C#, the static keyword has many uses and can be used to declare static members, static classes, and static methods.

  1. Static members: Members declared using the static keyword belong to the class, not to instances of the class. Static members are initialized before the class is used for the first time, and only one copy exists in memory, which can be accessed directly through the class name without creating an instance of the class. Static members can be static fields, static properties, and static methods.

  2. Static class: A class declared using the static keyword is a static class. Static classes contain only static members and cannot be instantiated or inherit from other classes. Static classes are usually used to provide a set of related static methods, or as utility classes to provide some commonly used functions.

  3. Static method: A method declared using the static keyword is a static method. Static methods do not need to be called through an instance of the class, but can be called directly through the class name. Static methods are usually used to provide some utility methods or perform some operations without instantiating the class.

Static members and static classes have only one copy that exists throughout the lifetime of the application and can be used without creating an instance of the class. Static members can be used to store and share global data, or provide some common functional methods. However, it should be noted that excessive use of static members may lead to code that is difficult to test, difficult to maintain, and highly coupled, so it should be used with caution.

Here is some example code using static in C#:

  1. Use of static members:
public class MyClass
{
    
    
    public static int MyStaticField; // 静态字段

    public static int MyStaticProperty {
    
     get; set; } // 静态属性

    public static void MyStaticMethod() // 静态方法
    {
    
    
        Console.WriteLine("This is a static method.");
    }
}

public class Program
{
    
    
    static void Main(string[] args)
    {
    
    
        MyClass.MyStaticField = 10; // 直接访问静态字段
        Console.WriteLine(MyClass.MyStaticField);

        MyClass.MyStaticProperty = 20; // 直接访问静态属性
        Console.WriteLine(MyClass.MyStaticProperty);

        MyClass.MyStaticMethod(); // 直接调用静态方法
    }
}
  1. Use of static classes:
public static class MathUtility
{
    
    
    public static int Add(int a, int b)
    {
    
    
        return a + b;
    }

    public static int Multiply(int a, int b)
    {
    
    
        return a * b;
    }
}

public class Program
{
    
    
    static void Main(string[] args)
    {
    
    
        int sum = MathUtility.Add(2, 3); // 直接调用静态类的方法
        Console.WriteLine(sum);

        int product = MathUtility.Multiply(4, 5);
        Console.WriteLine(product);
    }
}

In the above example, we have used static members and static classes. Through static members, we can directly access and manipulate static fields, properties and methods. Static classes provide a set of related static methods, which we can call directly through the class name.

Guess you like

Origin blog.csdn.net/qq_41942413/article/details/133328835