- Generic classes are type-safe compared to usual collections.
- There exists a generic equivalent to almost all the collections discussed in earlier chapters. e.g. ArrayList as List<>, etc.
- Generic List<> collection allows an overload of Sort( ) method to supply a generic delegate for comparison.
- One difference between generic Dictionary<> class and its non-generic counterparts is that it does not use DictionaryEntry objects to hold items, instead it uses generic KeyValuePair.
- Generic LinkedList<> is new in .NET 2.0. It consists of LinkedListNode objects and uses ILinkedListEnumerator.
- Using foreach on a LinkedList will not return LinkedListNode type objects. Instead it will return objects of type that is used to store value in each LinkedList node. Explaination code on Page 255 of MS Press book.
- Generic collections support generic interfaces, generic enumerators and generic comparisons for type-safety.
- Three classes are common to almost all collections in System.Collections namespace (and any of its sub-namespaces), namely, CollectionBase, ReadOnlyCollectionBase and DictionaryBase.
- From .NET 2.0 onwards, its preferrable to use generic collections than non-generic ones.
Showing posts with label Collections. Show all posts
Showing posts with label Collections. Show all posts
Monday, June 15, 2009
Chapter 4-Lesson 5
Chapter 4-Lesson 4
- BitArray class stores Boolean values in a resizable list. (Manually resizable using Length property)
- This class also supports standard operations such as AND, OR, XOR, etc.
- BitVector32 is a structure. It is not resizable, and is used to manipulate bits of, say an Int32.
- BitArray does not support Add( ) / Remove( ) methods. We use indexers instead.
- BitVector32 structure actually stores its value in its Data property which is an Int32. Any changes made to the bits would ultimately change the Data property.
- BitVector32 works on unsigned integers.
- Practical usage of BitVector32 include Bit Masking, Bit Packing, etc.
- Bit Packing allows storage of usual small numbers into one relatively large number to save space. (e.g. instead of using three Int16 numbers, we may pack them into one Int32)
- StringCollection and StringDictionary are strongly typed.
- StringCollection works identical to ArrayList, and StringDictionary works identical to HashTable except that both Key and Value must be strings.
- However, Keys in StringDictionary are case-Insensitive, meaning "fourth" and "Fourth" would lead to same Value.
- The CollectionsUtil class provides static methods for creating case-insensitive HashTable/SortedList.
- StringComparer object allows for creating Culture-Invariant collections.
- The NameValueCollection allows to store multiple Values per Key. Values can be retrieved by using Key or Index.
- This collection would behave like previous collections when items are added using indexer, but behaves differently (allows multiple Values per Key) when Add( ) method is used.
- It also supports retriving Value using Index number of Key. If a key has more than one Value, using index would return a comma seperated list.
Sunday, June 14, 2009
Chapter 4-Lesson 3
- Dictionary classes in .NET framework are used to map a Key to a Value.
- Useful when creating Lookup tables.
- Dictionaries can also use indexers.
- Objects in Dictionaries are typically of type DictionaryEntry, which hold a key and a value.
- All Dictionary classes derive from IDictionary which in turn derives from ICollection interface.
- The ContainsKey( )/ContainsValue( ) methods in HashTable are used to know the existance of specific Keys/Values in a HashTable collection.
- HashTable uses unique Hash values to store Values. So using the same Key for different values over time would retain just the latest because all others would overwrite each other.
- Hash values are internally integers.
- When using strings as Key, the string class overrides the GetHashCode( ) and Equals( ) method of the object class to make sure two similar strings in the Key specification get to the same Hash value in the collection (keep in mind strings are by default immutable)
- Therefore, if we use our objects' reference as Key, we might want to override these methods to make sure two instances with some property similar between them would be treated as the same Key point in the collection. (See Page 214-215 [Fish class example] in MS Press Book)
- The IComparer interface allows for sorting of objects of a class.
- We may supply object from a class that implements IEqualityComparer in the constructor of a HashTable. IEqualityComparer provides the same two methods discussed above to provide results of comparison in HashTable. Details on Page 216.
- HashTable returns items in the order of their Hash Values while traversal.
- SortedList collection returns objects in the sorted order. Sorting takes place with respect to Key.
- The indices of the DictionaryEntry objects may change as items are added or removed.
- HashTable is not good (in performance) for small collections (fewer than ten elements). Instead, use ListDictionary.
- HybirdDictionary starts as a ListDictionary and, as the data grows converts itself into a HashTable. Usage is same as HashTable.
- HashTable does NOT allow access to items by index.
- OrderedList class is used to attain performance of HashTable in large data scenario and at the same time allow the use of index for data items. Therefore it has extra methods that allow for insertion and removal as well as accessinf of data at specific index.
Labels:
Collections,
Dictionary,
Equals,
HashTable,
Key,
Value
Saturday, June 13, 2009
Chapter 4-Lesson 2
- Queue collection allows to Enqueue duplicate and null values.
- Queue is a FIFO collection while Stack is a LIFO collection.
- Queue supports Enqueue( ) and Dequeue( ) methods.
- Stack supports Push( ) and Pop( ) methods.
- Both classes support Peek( ) method.
Chapter 4-Lesson 1
- Collections are classes that support gathering of data in an orderly manner.
- SortedList is a name/value pair collection.
- BitArray is compact collection of Boolean values
- HybirdDictionary collection uses ListDictionary for small group of items and migrates to Hashtable when data size becomes large.
- The process when ValueTypes are wrapped up in object type is called Boxing.
- The AddRange( ) method of ArrayList supports every object that has implemented ICollection interface.
- Add( ) and AddRange( ) methods add objects at the end of ArrayList. For adding items at specific positions, use Insert( )/InsertRange( ) methods.
- The Remove( ) method will search for a given object in ArrayList, if found, will delete it, and if not found, will return without throwing Exception.
- ArrayList supports indexers, so we can use them as arr[2] = "something";
- For details on IEnumerator and how to use it, refer to MS Press book, Table 4-2 onwards.
- An alternative to using IEnumerable properties is to use the foreach construct.
- foreach may be used on any collection that supports IEnumerable.
- If we know the data type of the items in a specific collection, we may specify that in the foreach loop and it will automatically handle Unboxing.
- ICollection interface derives from IEnumerable interface. For details refer to MS Press book, Table 4-4 onwards.
- The CopyTo( ) method of the ICollection ensures that the contents of the collection can be converted into an Array.
- Simple collections such as ArrayList inherit from IList which in turn derives from ICollection. This adds the capability of methods such as Add( ), Clear( ), Contains( ), etc. and properties such as Item.
- The Sort( ) method in ArrayList uses Comparer class (which implements ICompaarer). So it also allows us to supply a Comparer object incase we dont wish to use the default.
Subscribe to:
Posts (Atom)