Array versus List: When to use which?

2018-12-31 03:39发布

MyClass[] array;
List<MyClass> list;

What are the scenarios when one is preferable over the other? And why?

标签: .net arrays list
15条回答
呛了眼睛熬了心
2楼-- · 2018-12-31 04:17

Notwithstanding the other answers recommending List<T>, you'll want to use arrays when handling:

  • image bitmap data
  • other low-level data-structures (i.e. network protocols)
查看更多
心情的温度
3楼-- · 2018-12-31 04:17

Rather than going through a comparison of the features of each data type, I think the most pragmatic answer is "the differences probably aren't that important for what you need to accomplish, especially since they both implement IEnumerable, so follow popular convention and use a List until you have a reason not to, at which point you probably will have your reason for using an array over a List."

Most of the time in managed code you're going to want to favor collections being as easy to work with as possible over worrying about micro-optimizations.

查看更多
呛了眼睛熬了心
4楼-- · 2018-12-31 04:20

It completely depends on the contexts in which the data structure is needed. For example, if you are creating items to be used by other functions or services using List is the perfect way to accomplish it.

Now if you have a list of items and you just want to display them, say on a web page array is the container you need to use.

查看更多
登录 后发表回答