Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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#?

like image 823
StuperUser Avatar asked Dec 27 '22 19:12

StuperUser


1 Answers

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.)

like image 72
LukeH Avatar answered Jan 03 '23 03:01

LukeH