What is the difference between Array and Collection?


(afree) #1

What’s the difference between Array and Collection In C# .NET Framework. And when is best to use which one?

(Luigi Di Biasi) #2

Hi @afree,
maybe, the question could not well formed.

When you talk about Array, you are talking about one of the core object in the .NET framework. Specifically, you are talking about the object on which at least all other object of the Collection namespace lies.

The Array is just one of the Collection types that .NET provides to the programmer.

In the specific .NET case, Array object provides the primary method to add, remove, update, sort and search item on vector and matrix. Then, it provides only basic functions with which you can manipulate vectors and matrices.

Regard to the kind of data that you have to store, you can use one of other type of Collections that .NET provide to you. For example, if you need to store a set of value and for each of them you want to assign a name you could use a Dictionary. Also, if you only need to store a set of values and you have not need of naming feature you could use Array or ArrayList!

Another example is the HashSet class: If you need to store a set of unique value, you have to use HashSet. This object will ensure to you that only one element of the same kind will be store in the Collections.