C# 数组与通用列表
我基本上想知道在下面提到的场景中使用通用列表而不是数组的差异或优点,
class Employee
{
private string _empName;
public string EmpName
{
get{ return _empName; }
set{ _empName = value; }
}
}
1. Employee[] emp
2. List<Employee> emp
有人可以告诉我优点或缺点以及更喜欢哪一个吗?
i basically want to know the differences or advantages in using a generic list instead of an array in the below mentioned scenario
class Employee
{
private string _empName;
public string EmpName
{
get{ return _empName; }
set{ _empName = value; }
}
}
1. Employee[] emp
2. List<Employee> emp
can anyone please tell me the advantages or disadvantages and which one to prefer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
一个很大的区别是
List
可以扩展(您可以对其调用 Add)或收缩(您可以对其调用 Remove),而 Employee[] 的大小是固定的。因此,除非需要,否则Employee[]
更难使用。One big difference is that
List<Employee>
can be expanded (you can call Add on it) or contracted (you can call Remove on it) whereas Employee[] is fixed in size. Thus,Employee[]
is tougher to work with unless the need calls for it.最大的区别是数组一旦创建就不能再变长或变短。但是,列表实例可以添加或删除元素。还有其他差异(例如可用的不同方法集),但添加/删除是最大的差异。
我喜欢 List,除非有充分的理由使用 Array,因为 List 的灵活性很好,而且相对于代码通常执行的大多数其他操作的成本而言,性能损失非常小。
如果您想深入了解许多有趣的技术细节,请查看 此 StackOverflow 线程 更深入地研究了列表与数组的问题。
The biggest difference is that arrays can't be made longer or shorter once they're created. List instances, however can have elements added or removed. There are other diffs too (e.g. different sets of methods available) but add/remove is the big difference.
I like List unless there's a really good reason to use an Array, since the flexibility of List is nice and the perf penalty is very small relative to the cost of most other things your code is usually doing.
If you want to dive into a lot of interesting technical detail, check out this StackOverflow thread which delves into the List vs. Array question in more depth.
使用通用列表,您可以便宜地
添加
/删除
等(至少在远端)。调整数组大小(添加/删除)的成本更高。明显的缺点是列表有空闲容量,因此可能会浪费一些字节 - 但在大多数情况下不值得担心(并且您可以修剪它)。一般来说,除非您知道您的数据永远不会改变大小,否则更喜欢列表。
从 API 角度来看,由于 LINQ,它们之间几乎没有什么可供选择的(即
List
上的额外方法很大程度上被 LINQ 复制,因此数组可以免费获得它们)。另一个优点是,使用列表,您不需要公开 setter:
消除一系列
null
错误,并允许您保持对数据的控制(特别是如果您使用不同的IList<>
实现,支持更改内容时的检查/验证)。With the generic list, you can
Add
/Remove
etc cheaply (at least, at the far end). Resizing an array (to add/remove) is more expensive. The obvious downside is that a list has spare capacity so maybe wastes a few bytes - not worth worrying about in most cases, though (and you can trim it).Generally, prefer lists unless you know your data never changes size.
API-wise, since LINQ there is little to choose between them (i.e. the extra methods on
List<T>
are largely duplicated by LINQ, so arrays get them for free).Another advantage is that with a list you don't need to expose a setter:
eliminating a range of
null
bugs, and allowing you to keep control over the data (especially if you use a differentIList<>
implementation that supports inspection / validation when changing the contents).如果要在公共接口中公开集合,.NET Framework 指南建议使用 List 而不是 T[]。 (事实上,BindingList)
在内部,如果您有一个固定的已知大小的集合,则数组可能更合适。与在列表末尾添加元素相比,调整数组大小的成本较高。
If you are exposing a collection in a public interface the .NET Framework Guidelines advise to use a List rather than T[]. (In fact, a BindingList< T >)
Internally, an array can be more appropriate if you have a collection which is a fixed, known size. Resizing an array is expensive compared to adding an element to the end of a List.
您需要在创建数组时知道数组的大小,但创建后无法更改其大小。
因此,它在创建时对数组使用动态内存分配。 (这与用于 C++ 数组的静态内存分配不同,后者的大小必须在编译时已知。)
列表可以在创建后动态增长,并且它具有
.Add()< /code> 函数来执行此操作。
-来自 MSDN
更喜欢哪一个?
列表
。You need to know the size of an array at the time that it is created, but you cannot change its size after it has been created.
So, it uses dynamic memory allocation for the array at creation time. (This differs from static memory allocation as used for C++ arrays, where the size must be known at compile time.)
A list can grow dynamically AFTER it has been created, and it has the
.Add()
function to do that.-from MSDN
Which one to prefer?
List<T>
.如果您知道数组的元素数量,那么数组是一个不错的选择。如果不使用该列表。在内部
List
使用 T 数组,因此它们实际上比您想象的更相似。If you know the number of elements array is a good choice. If not use the list. Internally
List<T>
uses an array of T so the are actually more like than you may think.使用列表,您不需要事先知道数组的大小。您可以根据实施的需要动态添加新员工。
With a List, you don't need to know the size of the array beforehand. You can dynamically add new Employee's based on the needs of your implementation.