list[i] 是 C# 中 list.get_item(i) 的别名吗?
我将 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,一般来说,属性只是
get_PropertyName
和set_PropertyName
方法的语法糖。索引器 - 例如,
list[i] ——只是一种特殊类型的属性,基本上是围绕
get_Item(i)
和set_Item(i)
方法的语法糖。(请注意,索引器属性不一定必须称为
Item
,但这就是它在List
上的名称,这也是自定义类型索引器的默认名称,除非您使用IndexerNameAttribute
。)Yes, properties in general are just syntactic sugar around
get_PropertyName
andset_PropertyName
methods.Indexers -- for example,
list[i]
-- are just a special type of property, basically syntactic sugar aroundget_Item(i)
andset_Item(i)
methods.(Note that the indexer property doesn't necessarily have to be called
Item
, but that's what it's called onList<T>
, and that's the default name given to indexers on custom types too unless you override it usingIndexerNameAttribute
.)List
的文档说是这样的:所以是的,
list[i]
是索引器,它是默认属性,在本例中为Item< /代码>。它将
获取
或设置
Item[i]
,具体取决于上下文是读取还是写入。另请参阅:索引器
The documentation for
List<T>
says it this way:So yes,
list[i]
is the indexer, which is the default property, which in this case isItem
. It willget
orset
Item[i]
depending on whether the context is reading or writing.See also: Indexers
方括号可以重载,它们称为索引器。所以我们需要知道
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()
.未必。 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.