C# what is the point or benefit of an indexer?

后端 未结 11 2094
暖寄归人
暖寄归人 2020-12-16 16:07

Doing some code reading and stumbled upon this snippet that I haven\'t seen before:

public SomeClass {
  public someInterface this[String strParameter] {
            


        
11条回答
  •  攒了一身酷
    2020-12-16 16:31

    It's an operator overload. Useful if you are writing a collection class for example where it would make sense to access it using array notation, e.g. collection[someIndex].

    You could of course write a collection.GetElement(someIndex) function equivalent, but it's a style/readability thing.

提交回复
热议问题