620 likes | 642 Views
Evolution of .NET Collections (C#). Chen Dong 2010.6.24. Roadmap of C# & Collections. Dynamic Programming – Concurrent Collections. C#4.0. Language Integrated Query. C#3.0. Generics. C#2.0. Managed Code – Collection Essential. C#1.0. .NET 1.1 Collection Essential. .NET 1.1 Collections.
E N D
Evolution of .NET Collections (C#) Chen Dong 2010.6.24
Roadmap of C# & Collections Dynamic Programming – Concurrent Collections C#4.0 Language Integrated Query C#3.0 Generics C#2.0 Managed Code – Collection Essential C#1.0
.NET 1.1 Collections • Namespaces • Mscorlib.dll: • System.Collections • System.dll: • System.Collections • System.Collections.Specialized
.NET 1.1 Collections • Fundamental Interfaces Why do we need to create various interfaces? Interface Segregation Principle!
.NET 1.1 Collections • Fundamental Interfaces • IEnumerator - A Forward Const Iterator interface IEnumerator { object Current {get;} bool MoveNext(); void Reset(); } • "Forward" means enumerator can move in only one direction - forward. • "Const" means that it cannot change the underlying collection. Current property is read-only.
.NET 1.1 Collections • Fundamental Interfaces • IEnumerable - An Enumerable Collection With Read-Only Forward-Only Access interface IEnumerable { IEnumerator GetEnumerator(); } • IEnumerable is implemented by virtually all .NET collections. GetEnumerator() returns an enumerator for the collection. • Any object that implements IEnumerable can be used with the foreach operator.
.NET 1.1 Collections • Fundamental Interfaces • Iterator Pattern • Single-Responsibility Principle Inside • Why does .NET introduce two interfaces to implement Iterator Pattern? To separate Enumerator
.NET 1.1 Collections • Fundamental Interfaces • ICollection - Synchronizable Collection With Count interface ICollection : IEnumerable { int Count {get;} bool IsSynchronized {get;} object SyncRoot {get;} void CopyTo(Array array, int Index ); } • SyncRoot : Gain the object used for synchronization
.NET 1.1 Collections • Fundamental Interfaces • IList - Modifiable Collection with Integer Index interface IList : ICollection { bool IsFixedSize { get; } bool IsReadOnly { get; } objectthis[ int Index ] {get; set;} int Add( object value ); void Clear(); bool Contains( object value ); int IndexOf( object value ); void Insert( int Index, object value ); void Remove( object value ); void RemoveAt( int Index ); } • Array implements IList, but class SortedList does not. • Because of the integer index, IList behaves more like an array rather than a list. • .NET does not provide operation complexity guarantees for the methods of IList. In particular, it is not formally guaranteed that all IList implementation will perform indexed access in constant time, or even in amortized constant time.
.NET 1.1 Collections • Fundamental Interfaces • IDictionary - Associative Container interface IDictionary : ICollection { bool IsFixedSize { get; } bool IsReadOnly { get; } objectthis[ object key ] {get; set;} ICollection Keys{ get; } ICollection Values{ get; } void Add( object key, object value ); void Clear(); bool Contains( object value ); IDictionaryEnumerator GetEnumerator(); void Remove( object key ); } • ICollection.GetEnumerator returning IEnumerator and IDictionary.GetEnumerator returning IDictionaryEnumerator are two different, unrelated methods. • IDictionary defines an associative container that stores key and value pairs. The interface is relatively straightforward. IDictionaryEnumerator is a helper interface that derives from IEnumerator.
.NET 1.1 Collections • Classes • System.Array : IList - Fixed Size Array • This class is an abstract base class for all built-in C# arrays. It represents an array of fixed size and implements IList interface. • It’s a special class. Built-in C# arrays derive from it implicitly. But user created class can not derive from it - this causes a compilation error. • public abstract class Array : ICloneable, IList, ICollection, IEnumerable • Array implements IList, and IList derives from ICollection. Why does Array still explicitly announces the implementation of ICollection? • Array implements IList. Why can’t it invoke Add()? Interface Flatten Array explicitly implements IList.Add()
.NET 1.1 Collections • Classes • ArrayList : IList - Dynamic Array • ArrayList is heterogenous - it can contain any object. • ArrayList can work as an adaptor for arbitrary IList. • ArrayList.Adapter(IList). • ArrayList uses Array to store items internally (unless it acts as adapter). • Array capacity is automatically doubled when more room is needed for elements. • Array capacity is never automatically reduced when removing elements. • Add() takes amortized constant time. • Insert() in the beginning of the array takes linear time. • Sort() uses QuickSort(), which is quadratic in the worst case.
.NET 1.1 Collections • Classes • BitArray : ICollection - Array of Bits • This class implements a fixed-sized array of Boolean values, or bits. Note that it implements merely an ICollection - not a full IList. Once constructed, the size of the bit array never changes. Besides, it does not implement IList methods such as IndexOf(), Contains(), etc. From the other hand it implements a number of additional, Boolean-specific methods such as And(), Or(), and Xor()
.NET 1.1 Collections • Classes • HashTable : IDictionary • Hash table is an unordered collection of key-value pairs. • Key objects must correctly implement Equals() and GetHashCode. • Hashtable allows you to specify your own hash code provider, and your own load factor.
.NET 1.1 Collections • Classes • SortedList : IDictionary • Sorted list is a sorted collection of key-value pairs. • Besides implementing IDictionary interface, it also allows access to its items by integer index via GetByIndex() and SetByIndex() methods. • A SortedList object internally maintains two arrays to store the elements of the list. • Operations on a SortedList object tend to be slower than operations on a Hashtable object because of the sorting. However, the SortedList offers more flexibility by allowing access to the values either through the associated keys or through the indexes.
.NET 1.1 Collections • Classes • ReadOnlyCollectionBase : ICollection • Provides the abstract base class for a strongly typed non-generic read-only collection. • This class makes the underlying collection available through the InnerList property, which is intended for use only by classes that are derived directly from ReadOnlyCollectionBase. • The derived class must ensure that its own users cannot modify the underlying collection.
.NET 1.1 Collections • Classes • CollectionBase : IList • Provides the abstract base class for a strongly typed collection. • Internally CollectionBase is a wrapper around ArrayList. • Potentially type-unsafe calls such as Insert() are implemented in two steps. • First virtual method OnInsert() is called. This method should check the validity of the inserted object (e.g. whether it belongs to the collection type). • If OnInsert() does not throw, CollectionBase proceeds with calling Insert() on the underlying ArrayList.
.NET 1.1 Collections • Classes • DictionaryBase : IDictionary • Provides the abstract base class for a strongly typed collection of key/value pairs. • Internally DictionaryBase relies on a HashTable. • The idea is similar to CollectionBase.
.NET 1.1 Collections • Classes • System.Collections.Specialized Classes • StringCollection • StringDictionary • NameObjectCollectionBase • NameValueCollection • OrderedDictionary • ListDictionary • HybridDictionary • BitVector32
.NET 1.1 Collections • Collection Operations • Comparison • Sort • Search • Convert • Synchronization
.NET 1.1 Collections • Comparison • IComparable • int CompareTo(object obj); • Compares the current instance with another object of the same type • The role of IComparable is to provide a method of comparing two objects of a particular type • IComparer • int Compare(object x, object y); • Compare two given objects • The role of IComparer is to provide additional comparison mechanisms
.NET 1.1 Collections • Sort • Sortable Collections: • Array, ArrayList • SortedList is sorted automatically, but you can specify the comperer in constructor • Sort() – • use default comparer • Sort(IComparer comparer) – • use specified comparer • Sort(int index, int count, IComparer comparer) – • use specified comparer and sort the elements in a range
.NET 1.1 Collections • Search • Searchable Collections: • Array, ArrayList • BinarySearch(object value) – • use default comparer • BinarySearch(object value, IComparer comparer) – • use specified comparer • BinarySearch(int index, int count, object value, IComparer comparer) – • use specified comparer and search the elements in a range
.NET 1.1 Collections • Convert • Convert an ArrayList into an Array • void CopyTo(Array array) • void CopyTo(Array array, int arrayIndex) • void CopyTo(int index, Array array, int arrayIndex, int count) • object[] ToArray() • Array ToArray(Type type) • Add an Array to an ArrayList • void AddRange(ICollection c)
.NET 1.1 Collections • Synchronization Hashtable ht = Hashtable.Synchronized(new Hashtable()); Synchronized() creates a synchronized (thread-safe) wrapper for the Hashtable: SynchHashtable which derives from Hashtable
.NET 2.0 Collections • Namespaces • Mscorlib.dll: • System.Collections.Generic • System.Collections.ObjectModel • System.dll: • System.Collections.Generic
.NET 2.0 Collections • Generic Mapping • Collection<T> (not abstract) – CollectionBase • ReadonlyCollection<T> (not abstract) - ReadonlyCollectionBase • List<T> - ArrayList • Dictionary<TKey,TValue> - Hashtable • Queue<T>,Stack<T>,SortedList<T> - … • Several generic collection types that do not have nongeneric counterparts • LinkedList<T> is a general-purpose linked list that provides O(1) insertion and removal operations. • SortedDictionary<TKey,TValue> is a sorted dictionary with O(log n) insertion and retrieval operations, making it a useful alternative to SortedList. • KeyedCollection<TKey,TItem> is a hybrid between a list and a dictionary, which provides a way to store objects that contain their own keys.
.NET 2.0 Collections • Benefits of Generics • Type-safe at compile-time • Better performance, no inbox/outbox • Maximize code reuse
.NET 2.0 Collections • Generic Interfaces
.NET 2.0 Collections • Generic Interfaces • Why doesn’t IList<T> inherit from IList? • Against Liscov Substitution Principle (LSP)! • Why does IEnumerable<T> inherit from IEnumerable and IEnumerator<T> inherit from IEnumerator? • Their type parameters T are only used in “output” positions (return values), so that they are co-variant • Why does IEnumerator<T> extend IDisposable? • For the purpose to release resources at the end of the enumeration
.NET 2.0 Collections • Collection<T> & List<T> • List<T> is designed for speed and for use as an internal implementation detail • you shouldn’t expose List<T>, is because it exposes too many members, many of which are not relevant in most situations. • List<T> from a property won’t be able to get notified when the collection is modified. • DoNotExposeGenericLists fires when you publicly expose List<T> via a field, method, property, or parameter • Collection<T> is designed for extensibility. • Collection<T> provides 4 overridable methods; ClearItems, InsertItem, RemoveItem and SetItem, which allow a derived class to be notified when a collection has been modified.
.NET 2.0 Collections • Operations • Predicate<T> delegate that allows you to specify methods for searching the list • Find/Remove/Exists/TrueForAll • Action<T> delegate that represents methods that act on each element of the list • ForEach • Converter<TInput,TOutput> delegate that lets you define conversions between types • ConvertAll
.NET 2.0 Collections • Iterators • An iterator is a section of code that returns an ordered sequence of values of the same type. • An iterator can be used as the body of a method, an operator, or a get accessor. • The iterator code uses the yield return statement to return each element in turn. yield break ends the iteration. For more information • Multiple iterators can be implemented on a class. Each iterator must have a unique name just like any class member. • The return type of an iterator must be IEnumerable, IEnumerator, IEnumerable<T>, or IEnumerator<T>
.NET 2.0 Collections • Iterators • Enables you to support foreach iteration in a class or struct without having to implement the entire IEnumerable interface!
.NET 3.0 Collections • Collection Initializers
.NET 3.5 Collections • LINQ
.NET 3.5 Collections • Namespaces • System.Core.dll: • System.Linq
.NET 3.5 Collections • LINQ to Objects • If performance to the application is not so critical, you can choose LINQ.
.NET 3.5 Collections • LINQ Query Types • Method Syntax • Query Syntax • – will be translated into Method Syntax by compiler
.NET 3.5 Collections • Language Extensions to Support LINQ
.NET 3.5 Collections • Extension Methods • Enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. • Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. • An extension method will never be called if it has the same signature as a method defined in the type.
.NET 3.5 Collections • Extension Methods for LINQ • Some extension methods on IEnumerable<T> still return IEnumerable<T>, that’s why they can be chained, which is similar to LISP. • public static IEnumerable<TResult> Select<TSource, TResult>( • this IEnumerable<TSource> source, • Func<TSource, TResult> selector • ) • public static IEnumerable<TSource> Where<TSource>( • this IEnumerable<TSource> source, • Func<TSource, bool> predicate • )
.NET 3.5 Collections • Deferred Execution • Generally, LINQ uses deferred execution. Only the query is invoked (foreach) then it will be executed. • If you want to make it execute immediately, you can use ToList<>, ToArray<>, ToDictionary<> • var linqBooks = books.Where(book => book.Title.StartsWith("L")) • .OrderBy(book => book.Price) • .Select(book => new { key = book.Title, value = book.Price } • .ToList<Book>();
.NET 4.0 Collections • Namespaces • Mscorlib.dll • System.Collections.Concurrent • System.dll: • System.Collections.Concurrent
.NET 4.0 Collections • Concurrent Collections • The .NET Framework 4 introduces the System.Collections.Concurrent namespace, which includes several collection classes that are both thread-safe and scalable. • Multiple threads can safely and efficiently add or remove items from these collections, without requiring additional synchronization in user code.
.NET 4.0 Collections • Concurrent Collections