Is the order of elements on a C# List deterministic?

前端 未结 3 1118
[愿得一人]
[愿得一人] 2021-01-17 15:10

I\'ve always thought otherwise, but recently I had the need to know:

If I add elements to a list in a certain order, am I guaranteed to find then always on the same

3条回答
  •  执笔经年
    2021-01-17 15:37

    Yes; you control the ordering of a List.

    You can assume that any .NET collection with a list[int] indexer has a predictable ordering; otherwise, the numerical index wouldn't make any sense. By comparison, it's not possible to use a numerical index with a Dictionary, and when you enumerate a dictionary, the ordering isn't guaranteed.

提交回复
热议问题