What's the difference Between T[] and List<T>?
Actually I'm always using Generic Collections and I frequently use List<>.
Some scenarios I think that new List<string>()
is very ugly and I prefer to use string[]
, but I don't use it, because as far as I know, Generics has a better performance and therefore I use them.
Is string[], int[] or whatever non-generic-array harmful to the application?
I just wanna know what's really the difference, impact between arrays and generic collections.
edit:
let's fake a scenario
I must call this method, should I use string[]
or List<string>
? What's better?
static void PrintValues(IEnumerable<string> val开发者_StackOverflow社区ues) {
foreach(var value in values) {
Console.WriteLine(value);
}
}
The main difference is that you can add new elements to a List<T>
.
Internally List<T>
stores elements in an array of type T[]
and it just automatically allocates a larger array when you're adding new elements (or shrinks the array when you're removing elements). This means that the performance will be roughly similar. There is some minor indirection when using List<T>
, but the JITter may inline that.
The main reason for using List<T>
is that it gives you more functionality - you can add and remove elements.
EDIT In your example with PrintValues
, it doesn't really matter. It depends on the source of the data. If you just want to call it with some fixed arguments you can use arrays, because they're easier to create, e.g. new [] { "hello", "world" }
.
In most of the real-world scenarios, you'll be reading the data from somewhere - if you can get all data at once, then you can use arrays. If you'll reading them one-by-one then you'll use List<string>
so that you can add elements as you read them.
To answer your edited question, the answer is: it doesn't matter to the PrintValues
function. It will simply iterate.
What matters is the code that calls this function - how did it come up with the values to print? If they're a fairly static list of values, then using an array would be more efficient. If the collection needs to be built or otherwise manipulated, then a List<T>
(or Stack<T>
or whatever) might be better.
If you're using LINQ, you'll be more likely to use IEnumerable<T>
and to not care what type is actually implementing that interface.
The List<T>
is basically a wrapper of an array T[]
with additional methods and implicit implementation of IList<T>
.
Also, typeof(T[]).IsAssignableFrom(typeof(IList<T>)) == true
.
It seems to me that the performance of an array would be better than the performance of the list, because everything a list does, it does something to an array, and an array just does things to itself.
I always have this in consideration to choose between this two, and i'll add you a new one too.
- Array: I only use it for read only access because random access is direct.
- ArrayList: Uses an array internally growing as needed, so it has the advantage of easy random access, but if the array is full on an insertion it has to allocate a new one and copy all the elements.
- LinkedList: Really good for insertion, should be avoided if you want random access because it has to transverse all the elements until the one requested.
An array is a primitive language structure. That means that, theoreticaly, they are more efficient. On the other hand they will have less features, allow unwanted operations, have no interfaces, multiple implementations with interesting properties...
Unless you have very serious performance issues, use Collections, not arrays.
精彩评论