list[i] 是 C# 中 list.get_item(i) 的别名吗?

发布于 2024-11-13 09:49:14 字数 510 浏览 1 评论 0原文

我将 lambda 表达式作为参数传递。

在本例中,someObject 有一个名为 property 的属性,可通过 someObject.property 访问。

当我通过时: o => o.childListOfObjects[0].property

其中 childListOfObjects 是一个 List,并且

expression.Body 返回 o => o.childListOfObjects.get_Item(0).property

跳到最后:

list[i] 是 C# 中 list.get_item(i) 的别名吗?

I'm passing a lambda expression as a parameter.

In this case, someObject has a property called property accessible with someObject.property.

When I pass: o => o.childListOfObjects[0].property,

where childListOfObjects is a List<someObejct> and ,

expression.Body returns o => o.childListOfObjects.get_Item(0).property.

Skip to the end:

Is list[i] an alias for list.get_item(i) in C#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哀由 2024-11-20 09:49:14

是的,一般来说,属性只是 get_PropertyNameset_PropertyName 方法的语法糖。

索引器 - 例如,list[i] ——只是一种特殊类型的属性,基本上是围绕 get_Item(i)set_Item(i) 方法的语法糖。

(请注意,索引器属性不一定必须称为 Item但这就是它在 List上的名称,这也是自定义类型索引器的默认名称,除非您使用 IndexerNameAttribute。)

Yes, properties in general are just syntactic sugar around get_PropertyName and set_PropertyName methods.

Indexers -- for example, list[i] -- are just a special type of property, basically syntactic sugar around get_Item(i) and set_Item(i) methods.

(Note that the indexer property doesn't necessarily have to be called Item, but that's what it's called on List<T>, and that's the default name given to indexers on custom types too unless you override it using IndexerNameAttribute.)

青衫负雪 2024-11-20 09:49:14

List的文档说是这样的:

默认的 Item 属性(C# 中的索引器)用于检索项目

所以是的,list[i] 是索引器,它是默认属性,在本例中为 Item< /代码>。它将获取设置Item[i],具体取决于上下文是读取还是写入。

另请参阅:索引器

The documentation for List<T> says it this way:

The default Item property (the indexer in C#) is used to retrieve an item

So yes, list[i] is the indexer, which is the default property, which in this case is Item. It will get or set Item[i] depending on whether the context is reading or writing.

See also: Indexers

屌丝范 2024-11-20 09:49:14

方括号可以重载,它们称为索引器。所以我们需要知道 list 是哪个类的实例。

编辑:哎呀,没有看到该列表是一个列表。我不确定你的意思是什么,但没有 List.getItem()

The square brackets can be overloaded, they're called indexers. So we need to know what class list is an instance of.

EDIT: Whoops, didn't see that list is a List. I'm not sure what method you mean, though, there is no List<T>.getItem().

东走西顾 2024-11-20 09:49:14

未必。 List 定义了一个索引器,它很可能只会调用 get_item,但这并不能保证,索引器中可能定义了更多隐藏逻辑......但答案可能是肯定的。

not necessarily. List defines an indexer that will most likely just call the get_item but that's not guaranteed, there might be more hidden logic defined in the indexer... but the answer is probably yes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文