latest Post

Collections in C#

Collections in C#

“.NET” offers a variety of collections, such as ArrayList, Hashtable, queues, Dictionaries. Collections are abstractions of data algorithms. An ArrayList is an abstract dynamic array, a Hashtable collection abstracts a lookup table, a Queues collection abstracts queues and so on. In addition to that, collections implement the ICollection, IEnumerable and IClonable interfaces. The detailed specification for each collection is found under the System.Collection namespace.

ArrayList Collection

An ArrayList is a dynamic array and implements the IList interface. Each element is accessible using the indexing operator. While the traditional array has a fixed number of elements, in the case of Array List, elements can be added or removed at run time.

We can create an object of ArrayList using a general type of syntax as follows;

ArrayList obj = new ArrayList();

Here you can use the new keyword to create an instance of the ArrayList object. You don't need to specify the size. Once you have an empty ArrayList object, you can use the Add() method to add elements to it,as in,
obj.Add("item1"); 
obj.Add("2"); 
obj.Add("Delhi"); 
Each new item in the ArrayList is added to the end of the list, so it has the largest index number. If you wanted to add an item in the middle of the list, then you can use "Insert()" with a numeric argument as follows,
Obj.Insert(2,"item2"); 
You can also remove members of the ArrayList using either Remove() or RemoveAt() methods as in the following,
Obj.Remove("item1"); 
Obj.RemoveAt(3); 
Benefits of ArrayLists
Insert Elements: An ArrayList starts with a collection containing no elements. You can add them in any position as you choose them.

Automatic Resizing: you do not need to specify the array size; as you add elements, the array automatically ensures there is enough memory for the array.

Flexibility When Removing Elements: you can remove any element from an Arraylist very easily.
Limitations of ArrayLists

The flexibility of ArrayList comes at a cost of performance. Since memory allocation is a very expensive business, the fixed number of elements of the simple array makes it much faster to work with.

Note - ArrayLists are slower and more resource-intensive than a conventional array.

Simple ArrayList Example

The following example shows an array list with an #ff0000 size. Elements are added dynamically as required. We are adding elements using the "Add()" method as well as using the "Insert()" method at a specific location. Later we are displaying all the elements by iterating through the list.
using System; 
using System.Collections; 
namespace CollectionApp  

    class Program  
    { 
        static void Main(string[] args) 
        { 
            //Defining an ArrayList 
            ArrayList obj = new ArrayList(); 
            //Adding elements 
            obj.Add("India"); 
            obj.Add("USA"); 
            obj.Add("Russia"); 
            obj.Add("300"); 
            //Adding elements to specific position 
            obj.Insert(2, "Japan"); 
            //Accessing elements 
            for (int i = 0; i < obj.Count; i++)  
            { 
                Console.WriteLine("At Index[" + i + "]= " + obj[i].ToString()); 
            } 
            Console.WriteLine("____________"); 
            Console.WriteLine("Press any key"); 
            Console.ReadKey(); 
        } 
    } 
}

About Mallikarjun A

Mallikarjun A
Recommended Posts × +

0 comments:

Post a Comment