what is difference between string array and list of string in c#

前端 未结 5 1583
旧巷少年郎
旧巷少年郎 2020-12-08 02:00

I hear on MSDN that an array is faster than a collection.

Can you tell me how string[] is faster then List.

5条回答
  •  死守一世寂寞
    2020-12-08 02:35

    Arrays are a lower level abstraction than collections such as lists. The CLR knows about arrays directly, so there's slightly less work involved in iterating, accessing etc.

    However, this should almost never dictate which you actually use. The performance difference will be negligible in most real-world applications. I rarely find it appropriate to use arrays rather than the various generic collection classes, and indeed some consider arrays somewhat harmful. One significant downside is that there's no such thing as an immutable array (other than an empty one)... whereas you can expose read-only collections through an API relatively easily.

提交回复
热议问题