有人可以解释这个C#语法吗?

Pre*_*tel -2 c#

这里到底发生了什么:

public decimal[] Coefficients;
public decimal this[int i]
{
    get { return Coefficients[i]; }
    set { Coefficients[i] = value; }
}
Run Code Online (Sandbox Code Playgroud)

这是什么this服务?它是某种延伸decimal吗?

Lew*_*rin 11

这是一个索引器.

索引器允许类或结构的实例像数组一样被索引.索引器类似于属性,除了它们的访问器接受参数.

来自链接的MSDN的示例:

class SampleCollection<T>
{
    // Declare an array to store the data elements.
    private T[] arr = new T[100];

    // Define the indexer, which will allow client code
    // to use [] notation on the class instance itself.
    // (See line 2 of code in Main below.)        
    public T this[int i]
    {
        get
        {
            // This indexer is very simple, and just returns or sets
            // the corresponding element from the internal array.
            return arr[i];
        }
        set
        {
            arr[i] = value;
        }
    }
}

// This class shows how client code uses the indexer.
class Program
{
    static void Main(string[] args)
    {
        // Declare an instance of the SampleCollection type.
        SampleCollection<string> stringCollection = new SampleCollection<string>();

        // Use [] notation on the type.
        stringCollection[0] = "Hello, World";
        System.Console.WriteLine(stringCollection[0]);
    }
}
// Output:
// Hello, World.
Run Code Online (Sandbox Code Playgroud)