What is the use of Indexers?

前端 未结 5 682
野性不改
野性不改 2020-12-16 06:15

What is the real use of indexer in C#?

It looks pretty confusing as I am a new c# programmer. It looks like an array of objects but it\'s not.

5条回答
  •  执念已碎
    2020-12-16 06:57

    Any collection in .NET, whether they are ArrayLists or generic Lists, have, behind the scenes arrays in their underlying structure. Indexers are merely a way to change how regular array indexes work, whether by accepting strings as an input to look for a corresponding object with that property, or some other input. But, again, underneath all that syntactic sugar, it's still ends up with a representation to an array.

    This article might help explain how custom indexers are made.

    On your second point, Enums are integers. If you need character representations of enums, you're limited to string manipulation, or you can decorate your enums with appropriate attributes so that they will emit your desired string or character representation.

提交回复
热议问题