gpt4 book ai didi

c# - "this[0]"在 C# 中是什么意思?

转载 作者:IT王子 更新时间:2023-10-29 03:45:39 25 4
gpt4 key购买 nike

我在浏览一些库代码时看到了如下方法:

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

包含此方法的类不是列表或可迭代的东西,那么 this[0] 到底是什么意思?

最佳答案

寻找 indexer在类里面。

C# 允许您定义索引器以允许此类访问。

这是“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;
}
}

这是来自 the official language specification 的定义:

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 索引器 部分找到完整的定义。

关于c# - "this[0]"在 C# 中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16650223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com