&是什么QUOT;这[0]"在C#是什么意思? [英] What does "this[0]" mean in C#?

查看:175
本文介绍了&是什么QUOT;这[0]"在C#是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会通过一些库代码,看到的方法,如:

I was going through some library code and saw a method like:

public CollapsingRecordNodeItemList List
{
    get { return this[0] as CollapsingRecordNodeItemList; }
}

这包含此方法的类不是一个列表或东西可迭代的,所以究竟是什么此[0] 意思?

The class that contains this method is not a list or something iterable, so what exactly does this[0] mean?

推荐答案

寻找一个索引在类。

C#允许您定义索引允许这种访问的。

C# lets you define indexers to allow this sort of access.

下面是从SampleCollection的官方指南的例子。

Here is an example from the official guide for "SampleCollection".

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,后跟一个分隔符[和]之间写的参数列表的属性。该参数在索引器的访问器可用。对属性类似,索引器可以读写,只读,只写和索引器的访问器可以是虚的。

An indexer is a member that enables objects to be indexed in the same way as an array. An indexer is declared like a property except that the name of the member is this followed by a parameter list written between the delimiters [ and ]. The parameters are available in the accessor(s) of the indexer. Similar to properties, indexers can be read-write, read-only, and write-only, and the accessor(s) of an indexer can be virtual.

人们可以发现在部分充分和完整的定义规范的 10.9索引器

One can find the full and complete definition in section 10.9 Indexers of the specification.

这篇关于&是什么QUOT;这[0]"在C#是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆