[C#] Use this for extension methods and static classes and static members

2023, Week 30, Article 2. Give yourself a goal, and then insist that there will always be a receipt, if you don’t believe me, try it!
This article mainly briefly talks about using this for extension methods and static classes and static members

insert image description here

1. This extension

In C#, you can add new methods and properties to existing classes through extension methods and extension properties. Extension methods allow us to call them like instance methods, while extension properties allow us to use them like normal properties.

1. Extended conditions

To create extension methods and extension properties, the following conditions need to be met:

1) Create a static class and mark it as static.
2) Create a static method in that class that will be the extension method we want to add to the existing class. The first parameter of the method must be thismarked with the keyword and specify the type of the class to be extended. This parameter represents the instance we use the extension method.
3) For extension attributes, we can simulate the behavior of extension attributes by creating a static class and a static method. Inside the method, access to properties and return of values ​​can be achieved through the name and parameter list of the method.

2. Example code

Here is a simple example showing how to use extension methods and extension properties:

// 扩展方法
public static class StringExtensions
{
    
    
    public static bool IsNullOrEmpty(this string str)
    {
    
    
        return string.IsNullOrEmpty(str);
    }
}

// 扩展属性
public static class MathExtensions
{
    
    
    public static double PI
    {
    
    
        get {
    
     return 3.14159; }
    }
}

// 使用扩展方法和属性
public class Program
{
    
    
    static void Main(string[] args)
    {
    
    
        string str = "Hello, World!";
        Console.WriteLine(str.IsNullOrEmpty()); // 调用扩展方法

        Console.WriteLine(MathExtensions.PI); // 访问扩展属性
    }
}

In the above example, we have created a StringExtensionsclass that contains an extension method IsNullOrEmptyfor null checking of strings. We also created a MathExtensionsclass that contains an extension property PIto get the value of pi.

Note that to use extension methods and extension properties, you need to import the namespace containing these classes. In the above example, we need to import Systemthe namespace.

2. Static knowledge points

1. Basic concepts

1) In C#, a static class is a special type of class that contains only static members (static methods, static properties, static fields) and cannot be instantiated.
2) A static class cannot be used as a base class, nor can other classes be derived from it.
3) Static classes are mainly used to provide a set of related public functions, which can be accessed without instantiating the class.
4) They are usually used to implement utility classes, helper classes or contain methods that can only be called by the class itself.

2. Examples

Here is an example of a simple static class:

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

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

1) In the above example, MathUtilsit is a static class that contains two static methods Addand Multiply.
2) These methods can be called directly through the class name without creating an instance of the class.
3) The advantage of using a static class is that it can avoid the overhead of creating an instance, and organize related functions in one class, making the code clearer and easier to maintain.
4) Also, members in static classes are available throughout the application without passing instance parameters.
5) It should be noted that since static classes cannot be instantiated, members of static classes can only access static members and constants, and cannot access instance members or non-static members.
6) The members of the static class are called directly through the class name without using instance reference.

In summary, a static class is a special class that contains only static members, cannot be instantiated, and is used to organize related functionality. It plays an important role in providing common functions, avoiding the overhead of creating instances, etc.

3. Static members

A static class can contain the following types of members:

3.1, Static Fields (Static Fields)

Static fields are variables in a static class and they have only one copy throughout the application. Static fields can store shared data and are shared among all instances of the class. They are marked as statickeywords and can be directly accessed through the class name.

3.2. Static Properties

Static properties are used to expose data in static classes. They provide access to private static fields and allow read and write operations. A static property defines an accessor method for getting and setting the property's value.

3.3. Static Methods

Static methods are methods in a static class that can be called directly by the class name without creating an instance of the class. Static methods cannot access non-static members, but they can access other static members in the same class.

3.4. Static Constructors

A static constructor is a special method in a static class. They are called automatically when the class is loaded to perform initialization of static members. Static constructors have no parameters and cannot be called manually.

3.4. Static Events

Static events are used to define and handle events in static classes. They are similar to normal events, but are only accessible within static class scope.

3.6. Nested Types

Static classes can contain other classes, structures, enumerations, or interfaces, making these types local to the scope of the static class.

To sum up, members of a static class include static fields, static properties, static methods, static constructors, static events, and nested types. These members allow the functionality of a static class to be used directly without creating an instance of the class.

Guess you like

Origin blog.csdn.net/lmy_520/article/details/131854280