How can I create an ArrayList with a starting index of 1 (instead of 0)

后端 未结 8 627
春和景丽
春和景丽 2021-01-04 09:47

How can I start the index in an ArrayList at 1 instead of 0? Is there a way to do that directly in code?

(Note that I am asking for ArrayList

8条回答
  •  有刺的猬
    2021-01-04 10:09

    The obvious way to do it would be to wrap an ArrayList in your own implementation, and subtract 1 from the indexer in all operations that uses the index.

    You can in fact also declare an array in .NET, that has an arbitrary lower bound (Scroll down to the section "Creating Arrays with a Non-zero Lower Bound").

    With that said, please don't do it in production code. It matters to be consistent.

提交回复
热议问题