Pregled posta

Adresa bloga:


C# Collections

Collections are similar to arrays: they store lists of objects. Hwever they provide a lot of usefull features not found in arrays (resizing, sorting etc). Microsoft .Net Framework provides severaltypes of collection classes_

  • ArrayList Class -similar to array

    • Remove method - removes an element

    • Add method - adds a new element at the end

    • Insert method - inserts a new element somewhere in the middle

  • Stack Class - implements LIFO mechanism - like a stack

    • Push - adds an element at the beginning

    • Pop - removes an element from the end

  • Hashtable Class - associative array - using other data types as element key

    • ContainsKey - ckeck if the key is already taken

    • elements are stored in DictionaryEntry object - used in foreach loop

  • SortedList Class - similar to Hashtable - elements allways sorted (by using key)

Collections and arrays use similar syntax: elements can be added to collection in the following way:

SortedList peopleAge = new SortedList();
peopleAge["john"] = 31;
peopleAge["mary"] = 36;
peopleAge["James"] = 53;
peopleAge["Francesca"] = 18;

Iteration is done the same way as in arrays, with the difference that items are returned as DictionaryEntry, meaning that casting needs to be used.

Post je objavljen 18.09.2008. u 20:48 sati.