3
Concerning arrays and Collections in C#, vary from application to application or there is unanimity between them?
3
Concerning arrays and Collections in C#, vary from application to application or there is unanimity between them?
8
If there were unanimity there would not need to be both. The dichotomy between these two does not even make much sense.
Contrary to what people might think, a collection you’re talking about must be the ICollection
, And it’s something abstract, it doesn’t really exist. You use a specific collection, and there are several, so you should choose which one is best suited for you for each situation. There is no single solution, each data structure (and this is the subject you should study) has advantages and disadvantages. Spend your application at that moment whatever you choose.
In general you should never use array in C#. It is too concrete, has limitations and almost no relevant advantage. Almost all data structures that implement ICollection
has all relevant advantages of array and a few more. The use of array in modern and well written code should only use array because of legacy code or that has some specific performance issue.
Types that are collections usually have the same basic structure as the array, even internally has a array in many of them, but has the advantage of being able to do some operations that array cannot, for example, increase or reduce the size of the collection. You can see more about this in Difference between Icollection, Ilist and List?. Reading there, a collection does not guarantee that you can insert or remove items in the middle of the collection, just a list (IList
) allows it. Ok, it’s a collection too, but there are collections that do not have this feature.
A collection may or may not define types in them, but those that do not allow are considered obsolete and for all intents and purposes should not be used, forgets that they exist, are problematic. Even if you want to put any kind in the modern and efficient collection use the type object
. And even in this case think well if this is what you want, it’s almost always a mistake to do so. And you can do with array also, only use object[]
.
Nor does it make any sense what the other answer (there was one, and it was erased, most of this answer is contestation of what was wrong in the other) says about the elements being read-only or not. In raw loops in any case the structure is not explicitly readonly so much array how many collections can have their data changed normally, already in loops foreach
there are limitations on both for an iterator account.
It was also wrong that one of them is set at compile time and another running. They are equal, both are written and defined at compile time and both are instantiated at runtime, the difference is only how each can be manipulated, nothing more, including because most collections have a array internally instantiated.
Browser other questions tagged c# .net array collection
You are not signed in. Login or sign up in order to post.
class Array and interface Icollection.
– Augusto Vasques