Overview Of Array And Collection

What is an Array?

An array is a group of similar data types that is fixed in size. Under array, there is no process of boxing and unboxing because Array is Strongly typed.

Example

int[] A = new int[6] { 1, 2, 3, 4, 5, 6 };
for (int i = 0; i < 6; i++)
{
    Console.Write(A[i]);
}

In the above example, the array type is int, and the name of the array is A, which displays all the values of array A: 1,2,3,4,5,6.

Generally, a developer prefers to use Array because there is no boxing and unboxing. Suppose there is one School and the principal of the school would like to know only the name of the student of branch CSE, it is better to use an array because we know how many students are there in the CSE branch and also we know that the type of Student Name is a string.

string[] Student_Name = new string[60]
{
    "Amit", // ... Add other names here
    // ...
    "Sandep"
};

This will contain only the Student Name. There are the following three types of Array.

  1. Single Dimensional Array
  2. Multi-Dimensional Array
  3. Jagged Array

What is Collection?

Collection is a group of homogenous and heterogeneous data types. The size of the Array is not fixed, and also Collection is not a strong type. We use Generic to make Collection as Strong type. In C# we use namespace System.Collection.Generic to implement Generic on Collection.

Syntax

Collection<DataType> Name = new Collection<DataType>();

Generally, we use Collection when there is a Heterogeneous data type object. Suppose there is one School, and the principal of the school would like to know the Student ID, Student Name, and Course Name of a student of Branch CSE; it is better to use Collection because we have to display the data of Heterogeneous data type object. Like.

Collection<int> StudentIds = new Collection<int>();
Collection<string> StudentNames = new Collection<string>();
Collection<string> Courses = new Collection<string>();

What is a Recap Array?

Recap Array is a group of homogeneous data-type objects that are fixed in size, and this is strongly typed. A collection is a group of homogeneous and heterogeneous data-type objects that is not fixed in size and thus is not strongly typed. But we use Generic to make Collection a strong Type.

Difference between Array and Collection

Array Collection
1. Array is a Group of Homogeneous data type object. 1. Collection is a Group of Homogeneous and Heterogeneous data type object.
2. The array is fixed in size. 2. The collection is not fixed in size.
3. The array is a Strong type. 3. Collection is not a strong Type.
4. There is no boxing and unboxing process on Array 4. There is the process of Boxing and Unboxing on Collection.
5. We use Generic on Collection to make Collection as Strong type  

Note. The difference between Array and Collection/Array and Array list /Array and Data list will be the same.


Similar Articles