MyClass[] array;
List<MyClass> list;
当一个比另一个更可取的情况是什么?,为什么?
MyClass[] array;
List<MyClass> list;
当一个比另一个更可取的情况是什么?,为什么?
在现实中,很少需要使用数组。当您想添加/删除数据时,一定要使用List<T>,因为调整数组的大小是非常昂贵的。如果您知道数据是固定长度的,并且由于某些非常特定的原因(在基准测试之后)想要进行微观优化,那么数组可能会很有用。
List<T>提供了比数组更多的功能(尽管LINQ稍微均衡了一点),并且几乎总是正确的选择。当然,除了参数参数。- p;
当counter - List<T时,>是一维的;比如你已经有了int[,]或string[,,]这样的矩形数组,但是在对象模型中还有其他方法来建模这样的数据(如果你需要的话)。
参见:
如何/何时在c#.net中放弃使用数组? 数组,重点是什么?
也就是说,我在我的protobuf-net项目中大量使用数组;完全为了性能:
它做了很多位移动,所以字节[]对于编码非常重要; 我使用一个本地滚动字节[]缓冲区,我在发送到底层流(和v.v v.)之前填充;比BufferedStream等更快; 它在内部使用基于数组的对象模型(Foo[]而不是List<Foo>),因为大小一旦构建就固定了,并且需要非常快。
但这绝对是个例外;对于一般业务线处理,List<T>每次都胜出。
除非你真的关心性能,我的意思是,“你为什么使用。net而不是c++ ?”你应该坚持使用List<>。它更容易维护,并为您在幕后完成调整数组大小的所有繁琐工作。(如果有必要,List<>在选择数组大小方面非常聪明,所以通常不需要这样做。)
它完全取决于需要数据结构的上下文。例如,如果您正在创建供其他函数或服务使用的项,则使用List是完成该任务的最佳方式。
现在,如果你有一个项目列表,你只是想在网页上显示它们,数组是你需要使用的容器。
实际上,我只是想添加一个链接,我很惊讶还没有提到:Eric的Lippert的博客条目“数组被认为有点有害”。
您可以从标题中判断,它建议在任何可行的地方使用集合——但正如Marc正确地指出的那样,在很多地方,数组确实是唯一可行的解决方案。
大多数情况下,使用List就足够了。List使用内部数组来处理其数据,并在向List中添加比当前容量更多的元素时自动调整数组的大小,这使得它比需要事先知道容量的数组更容易使用。
有关c#中的列表的更多信息,请参阅http://msdn.microsoft.com/en-us/library/ms379570(v=vs.80).aspx#datastructures20_1_topic5,或者只是反编译System.Collections.Generic.List<T>。
如果需要多维数据(例如使用矩阵或图形编程),则可能使用数组。
像往常一样,如果内存或性能是一个问题,测量它!否则,您可能会对代码做出错误的假设。
Another situation not yet mentioned is when one will have a large number of items, each of which consists of a fixed bunch of related-but-independent variables stuck together (e.g. the coordinates of a point, or the vertices of a 3d triangle). An array of exposed-field structures will allow the its elements to be efficiently modified "in place"--something which is not possible with any other collection type. Because an array of structures holds its elements consecutively in RAM, sequential accesses to array elements can be very fast. In situations where code will need to make many sequential passes through an array, an array of structures may outperform an array or other collection of class object references by a factor of 2:1; further, the ability to update elements in place may allow an array of structures to outperform any other kind of collection of structures.
Although arrays are not resizable, it is not difficult to have code store an array reference along with the number of elements that are in use, and replace the array with a larger one as required. Alternatively, one could easily write code for a type which behaved much like a List<T> but exposed its backing store, thus allowing one to say either MyPoints.Add(nextPoint); or MyPoints.Items[23].X += 5;. Note that the latter would not necessarily throw an exception if code tried to access beyond the end of the list, but usage would otherwise be conceptually quite similar to List<T>.
当集合本身的不可变性是客户端和提供者代码之间契约的一部分时(不一定是集合中项目的不可变性)以及当IEnumerable不合适时,应该优先使用数组而不是List。
例如,
var str = "This is a string";
var strChars = str.ToCharArray(); // returns array
很明显,对"strChars"的修改不会改变原始的"str"对象,无论实现级是否了解"str"的底层类型。
但是假设
var str = "This is a string";
var strChars = str.ToCharList(); // returns List<char>
strChars.Insert(0, 'X');
在这种情况下,仅仅从代码片段中还不清楚insert方法是否会改变原始的“str”对象。它需要String的实现级知识来做出判断,这打破了契约式设计方法。在String的情况下,这不是一个大问题,但在几乎所有其他情况下,这可能是一个大问题。将List设置为只读确实有帮助,但会导致运行时错误,而不是编译时错误。
与其对每种数据类型的特性进行比较,我认为最实用的答案是“对于您需要完成的任务来说,差异可能并不那么重要,特别是因为它们都实现了IEnumerable,所以遵循流行的惯例,使用List,直到您有理由不使用List,此时您可能会有理由使用数组而不是List。”
大多数情况下,在托管代码中,您会希望集合尽可能易于使用,而不是担心微观优化。
Lists in .NET are wrappers over arrays, and use an array internally. The time complexity of operations on lists is the same as would be with arrays, however there is a little more overhead with all the added functionality / ease of use of lists (such as automatic resizing and the methods that come with the list class). Pretty much, I would recommend using lists in all cases unless there is a compelling reason not to do so, such as if you need to write extremely optimized code, or are working with other code that is built around arrays.
因为没有人提到:在c#中,数组是一个列表。MyClass[]和List<MyClass>都实现了IList<MyClass>。(例如void Foo(IList<int> Foo)可以像Foo(new[]{1,2,3})或Foo(new List<int>{1,2,3})一样调用)
因此,如果你正在编写一个接受List<MyClass>作为参数的方法,但只使用功能的子集,你可能想声明为IList<MyClass>,以方便调用者。
细节:
为什么数组实现IList? 如何在c#数组部分实现IList<T>?
它们可能不受欢迎,但我是游戏项目中的数组的粉丝。 -迭代速度在某些情况下是很重要的,如果你对每个元素不做太多操作,数组上的foreach的开销就会大大减少 -添加和删除helper函数并不难 -速度比较慢,但如果你只建了一次,那就无所谓了 -在大多数情况下,更少的额外内存被浪费(只有数组结构才真正重要) -稍微少一点垃圾和指针和指针追逐
话虽如此,在实践中,我使用列表的次数远多于数组,但它们都有各自的位置。
如果List是内置类型,那么他们就可以优化包装器和枚举开销。
填充列表比填充数组更容易。对于数组,您需要知道数据的确切长度,但对于列表,数据大小可以是任何大小。你可以把一个列表转换成一个数组。
List<URLDTO> urls = new List<URLDTO>();
urls.Add(new URLDTO() {
key = "wiki",
url = "https://...",
});
urls.Add(new URLDTO()
{
key = "url",
url = "http://...",
});
urls.Add(new URLDTO()
{
key = "dir",
url = "https://...",
});
// convert a list into an array: URLDTO[]
return urls.ToArray();
数组Vs.列表是典型的可维护性Vs.性能问题。几乎所有开发人员都遵循的经验法则是,您应该兼顾两者,但当两者发生冲突时,请选择可维护性而不是性能。该规则的例外情况是当性能已经被证明是一个问题时。如果你把这个原则应用到数组Vs.列表中,你会得到这样的结果:
使用强类型列表,直到遇到性能问题。如果遇到性能问题,请决定是否使用数组对解决方案的性能更有利,而不是在维护方面对解决方案造成损害。
请记住,使用List是不可能做到这一点的:
List<string> arr = new List<string>();
arr.Add("string a");
arr.Add("string b");
arr.Add("string c");
arr.Add("string d");
arr[10] = "new string";
它生成一个异常。
相反,使用数组:
string[] strArr = new string[20];
strArr[0] = "string a";
strArr[1] = "string b";
strArr[2] = "string c";
strArr[3] = "string d";
strArr[10] = "new string";
但是对于数组,不会自动调整数据结构的大小。您必须手动或使用Array管理它。调整方法。
一个技巧是用一个空数组初始化List。
List<string> arr = new List<string>(new string[100]);
arr[10] = "new string";
但在这种情况下,如果你使用Add方法添加一个新元素,它将被注入到列表的末尾。
List<string> arr = new List<string>(new string[100]);
arr[10] = "new string";
arr.Add("bla bla bla"); // this will be in the end of List