Friday, September 24, 2010

Working with Collection classes C Sharp

Working with Collections:
Dot net Base Class library provides number of collection class to work with. Generally collection classes are used to group related objects. The collection classes are designed in order to overcome the limitations of the arrays. Most of the collections classes have methods to add, delete, find, sort, reverse and many more. By using the collection classes we can increase the size dynamically and we can retrieve the information by using the key which is a string value, besides using the index. Most of the common collection classes are Stack, Heaps, Queue, Array List and Hashtable. Hashtable allows retrieving the information from the collection based key-value pair.

Working with ArrayList:
The ArrayList class is most commonly used collection class in the system.collections namespace.
Consider the previous example of categories and products, by using the ArrayList class we can work with the records of the customers more efficiently.
Common Methods used in ArrayList():
ArrayList list = new ArrayList ();
ADD():
Adding individual items to the array list.
list.Add(“FM Transmitter”);
list.Add(“DJ Cable”);
list.Add(“Adapter”);
AddRange():
To add a specified range of elements to array list.
string[ ] products = { "FM Transmitter ", "DJ Cable ", " Adapter " };
list.AddRange (products);

BinarySearch():
Is used to search entire list.
list.BinarySearch("FM Transmitter ") returns index value as 0.
 
Contains():
Is used to determine whether an element is in the list.
list.Contains("DJ Cable"). returns a true or false based on the parameter.

CopyTo():

copies entire list to the specified array, starting at the beginning of the target array.
string[ ] products = {“FM Transmitter ", "DJ Cable ", “Adapter " };
list.AddRange(products);           
string[ ] b = new string[3];
list.CopyTo(b);

GetRange():
GetRange method is used to extract the range of elements or to create a new collection of elements from an existing list.
string[,] c = new string[2,2] { {"hello","hi"},{"hello","hi"} };
list.Add(c);       
foreach (string [,]f in list.GetRange(0, 1))
        {
                Console.WriteLine("{0}.{1}",f[0,0],f[0,1]);
         }

Indexof():
 Is used to find the index of an element in the ArrayList.
list.IndexOf(" Adapter"); returns the zero based index value as 2.

Insert():
Is used to add an element at a specified index.
ArrayList list = new ArrayList();
list.Insert(0,"HeadPhoe");

InsertRange():
Is used to add a specified range of items to array list at a specified index:
string[ ] products = {“FM Transmitter ", "DJ Cable ", “Adapter " };
list.AddRange(products);           
list.InsertRange (0, products);
In order to add values to the categories, products and customer sections. We need to create classes for each one of the section. For time being we will create products class and category class.

Remove:
Is used to remove an item from the array depending on the given string value.
ArrayList list = new ArrayList();
list.Add("FM Transmitter");
list.Add("DJ Cable");
list.Add("Adapter");
list.Remove("Adapter");

RemoveAt:
Is used to remove an element from the array list at a specified index:
ArrayList list = new ArrayList();
list.Add("FM Transmitter");
list.Add("DJ Cable");
list.Add("Adapter");
list.RemoveAt(0)

Sort() and Reverse():
Is used to sort and reverse the array list.
ArrayList list = new ArrayList();
list.Add("FM Transmitter");
list.Add("DJ Cable");
list.Add("Adapter");
list.Sort();
list.Reverse();

Note:
Difference between Add and AddRange: If u r using Add method to add elements in the array list, each element occupies a single index and by if u r using AddRange method the collection of elements are inserted at a single index.

Using ArrayList to maintain product objects:
 For example consider u have an products class as follows,  u can work with it by using Array List class as follows:
public class products
    {
        private string id, name, price;
        // constructor
        public  products()
        {

        }
        public products(string _id, string _name, string _price)
        {
            id = _id;
            name = _name;
            price = _price;
        }
        public string ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        public string Price
        {
            get
            {
                return price;
            }
            set
            {
                price = value;
            }
        }
    }
Using ArrayList AddRange Method():
ArrayList products = new ArrayList();
products.AddRange(new products[] { new products("FMT","FM Transmitter","$43"),new products("DJC","DJ Cable","$54") });   

Using ArrayList Add Method():
ArrayList products = new ArrayList();
products p1 = new products("FMT", "FM Transmitter", "$43");
products p2 = new products("DJC", "DJ Cable", "$54");
products.Add(p1);
products.Add(p2);
Next :
Working with Stack.
Further Reading:
Msdn - Array List 

No comments:

Post a Comment