1.

What is the difference between an Array and ArrayList in C#?

Answer»

An array is a collection of similar variables clubbed together under one common name. While ARRAYLIST is a collection of OBJECTS that can be indexed individually. With ArrayList you can access a number of features like dynamic MEMORY allocation, adding, searching, and sorting items in the ArrayList. 

  • When declaring an array the size of the items is FIXED therefore, the memory allocation is fixed. But with ArrayList, it can be increased or DECREASED dynamically.
  • Array belongs to system.array namespace while ArrayList belongs to the system.collection namespace.
  • All items in an array are of the same datatype while all the items in an ArrayList can be of the same or different data types.
  • While arrays cannot accept null, ArrayList can accept null values.

For ex.:

// C# program to illustrate the ArrayListusing System;using System.Collections; class IB { // Main Method public static void Main(string[] args) { // Create a list of strings ArrayList al = new ArrayList(); al.Add("Bruno"); al.Add("Husky"); al.Add(10); al.Add(10.10); // Iterate list element using foreach loop foreach(var names in al) { Console.WriteLine(names); } }}


Discussion

No Comment Found