Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek

Similar presentations


Presentation on theme: "CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek"— Presentation transcript:

1 CHARLES UNIVERSITY IN PRAGUE http://d3s.mff.cuni.cz/~jezek faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek pavel.jezek@d3s.mff.cuni.cz Some of the slides are based on University of Linz.NET presentations. © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License (http://www.msdnaa.net/curriculum/license_curriculum.aspx)

2 IEnumerable and IEnumerator (optimized) following statement: foreach (ElementType element in collection) statement; is translated into: var enumerator = collection.GetEnumerator(); try { ElementType element; while (enumerator.MoveNext()) { element = (ElementType) enumerator.Current; statement; } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) disposable.Dispose(); }

3 Iterating Over a Tree class Node { public Node left; public Node right; public int value; public Node(Node left, Node right, int value) { this.left = left; this.right = right; this.value = value; } public Node(int value) : this(null, null, value) { } public IEnumerator GetEnumerator() { if (left != null) { foreach (int x in left) { yield return x; } yield return value; if (right != null) { foreach (int x in right) { yield return x; } class Program { static void Main(string[] args) { Node root = new Node( new Node( new Node(2), new Node(7), 5 ), new Node( new Node( new Node(14), new Node(17), 15 ), new Node(25), 20 ), 10 ); foreach (int x in root) { Console.Write("{0} ", x); } Console.WriteLine(); }

4 Iterating Over a Tree = “Counter-example” class Node { public Node left; public Node right; public int value; public Node(Node left, Node right, int value) { this.left = left; this.right = right; this.value = value; } public Node(int value) : this(null, null, value) { } public IEnumerator GetEnumerator() { if (left != null) { foreach (int x in left) { yield return x; } yield return value; if (right != null) { foreach (int x in right) { yield return x; } class Program { static void Main(string[] args) { Node root = new Node( new Node( new Node(2), new Node(7), 5 ), new Node( new Node( new Node(14), new Node(17), 15 ), new Node(25), 20 ), 10 ); foreach (int x in root) { Console.Write("{0} ", x); } Console.WriteLine(); } Generates new enumerator for each node in the tree!

5 Collection Classes.NET 1.0, 1.1 based on System.Object System.Collections namespace.NET 2.0, 3.0, 3.5, 4.0 generic classes System.Collections.Generic namespace

6 Interface ICollection : IEnumerable Interface ICollection : IEnumerable, IEnumerable Interface ICollection : IEnumerable Interface ICollection : IEnumerable, IEnumerable Basic interface for collections: New in ICollection : int Count { get; } number of elements bool IsSynchronized {get;} collection synchronised? object SyncRoot {get;} returns object for synchronisation void CopyTo(Array a, int index); copies the elements into array (starting at position index) void Add(T item); bool Remove(T item); void Clear(); bool Contains(T item;

7 Interface IList : ICollection, IEnumerable Interface IList : ICollection, IEnumerable, IEnumerable Interface IList : ICollection, IEnumerable Interface IList : ICollection, IEnumerable, IEnumerable Interface for object collections with a defined order interface IList { object this [ int index ] {get; set;} int Add(object value); void Insert(int index,object value); void Remove(object value); void RemoveAt(int index); void Clear(); bool Contains(object value); … }

8 Interface IDictionary Interface IDictionary Interface IDictionary Interface IDictionary interface IDictionary : ICollection, IEnumerable { ICollection Keys {get;}; ICollection Values {get;}; object this[object key] {get; set;} void Add(object key, object value); void Remove(object key); bool Contains(object key); IDictionaryEnumerator GetEnumerator(); … } interface IDictionary : IDictionary, ICollection >, IEnumerable >,... { … } public struct DictionaryEntry { public DictionaryEntry (object key, object value); public object Key {get;}; public object Value {get;}; }

9 Namespace System.Collections.Generic Classes List SortedList Dictionary SortedDictionary Stack Queue LinkedList HashSet SortedSet Interfaces ICollection IList IDictionary ISet IEnumerable IEnumerator IComparable IComparer IEquatable IEqualityComparer implemented as a reallocated T[] (corresponds to ArrayList) implemented as a pair of realloc. T[], U[] (corr. to SortedList) implemented as a hash table in an array (corr. to Hashtable) implemented as a red-black tree implemented in a reallocated T[] (corresponds to Stack) implemented as a circular queue in r. T[] (corresponds to Queue) doubly linked list of LinkedListNode (new in.NET 2.0) implemented as a hash table in an array + provides quick set operations (new in.NET 3.5 + implements ISet in 4.0) implemented as a red-black tree (new in.NET 4.0) (new in.NET 4.0)

10 Type Variance

11 Any Meaningful Application of the Type Below? struct X where T : class { private T t; public static implicit operator T(X x) { return x.t; } public static implicit operator X (T t) { X x; x.t = t; return x; }


Download ppt "CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek"

Similar presentations


Ads by Google