Fun with Lists for the Novice and Expert Scott Reed Brain Hz Software (760)
In the beginning… There were arrays… And there were System.Collections – ArrayList : IList : ICollection : IEnumerable – Hashtable : IDictionary : ICollection … – Various others Queue, Stack, SortedList
IEnumerable and IEnumerator Pull style iteration (better than Java :) IEnumerable – IEnumerator GetEnumerator() IEnumerator – bool MoveNext(); – object Current { get; } What foreach uses – foreach (object item in arrayList)
ICollection Count CopyTo SyncRoot and IsSynchronized*
IList Adding and removal – Add, Insert, Remove, RemoveAt Clearing (via Clear) Random access – has an indexer Finding – Contains, IndexOf Properties – IsFixedSize, IsReadOnly
IDictionary Adding and removal – Add, Remove and Clear only Random access – has an indexer Finding – Contains only Properties – IsFixedSize, IsReadOnly Collections – Keys and Values
Side note: What about Arrays? int[] : Array : IList : ICollection : IEnumerable Fixed size collections – Add and Remove not supported
What is the problem? Dangerous - Type safety Ugly - Casting Performance of value types (boxing and unboxing)
System.Collections.Generic ObjectGenerics IEnumeratorIEnumerator (inherit non generics) ICollectionICollection (modifyable - more like IList) IListIList (adds random access) IDictionaryIDictionary (adds TryGetValue) ArrayListList (still an array) HashtableDictionary SortedList Queue, Stack LinkedList, and LinkedListNode
Extras FindAll Sort FxCop warnings BindingLists ObservableCollections DataTable yield
Thank You Scott Reed Brain Hz Software (760)