Advanced .NET Programming I 2nd Lecture

Slides:



Advertisements
Similar presentations
Fun with Lists for the Novice and Expert Scott Reed Brain Hz Software (760)
Advertisements

Data Structures and Collections
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 14 th Lecture Pavel Ježek
Collections. 2 Objectives Explore collections in System.Collections namespace –memory management –containment testing –sorting –traversal.
Iterator Pattern Dr. Neal CIS 480. Iterator An iterator pattern can be used when one class is a collection of things and would like to provide a standardized.
Collection types Collection types.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th & 8 th Lecture Pavel Ježek.
Advanced .NET Programming I 13th Lecture
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
Copyright © 2006 Thomas P. Skinner1 Chapter 5 Indexers, Interfaces, and Enumerators.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
Advanced C# Types Tom Roeder CS fa. From last time out parameters difference is that the callee is required to assign it before returning not the.
Interface: (e.g. IDictionary) Specification class Appl{ ---- IDictionary dic; dic= new XXX(); application class: Dictionary SortedDictionary ----
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
Introduction to C# 2.0 An Advanced Look Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
C# Collections & Generics C#.NET Software Development Version 1.0.
Data Structures and Collections Principles.NET: –Two libraries: System.Collections System.Collections.Generics FEN 2014UCN Teknologi/act2learn1 Deprecated.
Generics & Collection Classes Version 1.0. Topics Generic Methods and Classes Generic Collection Classes List Enumerators Queue Stack LinkedList.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th Lecture Pavel Ježek
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
1 New Features in C# 2.0 Generic Types Iterators Simplified Delegates Anonymous Methods Partial Types Various © University of Linz, Institute for System.
C# Collections & Generics
8. Hafta İçeriği ArrayList TheArrayList class supports dynamic arrays, which can grow or shrink as needed. In C#, standard arrays are of a fixed length,
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 2 nd Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek
Sort & Search Algorithms
Linear Data Structures: Stacks and Queues
Advanced Data Collections
Computing with C# and the .NET Framework
Advanced .NET Programming I 3nd Lecture
Introduction to .NET Generics
Advanced .NET Programming I 4th Lecture
Iterators and Comparators
Chapter 5: Programming with C#
Week 15 – Monday CS221.
Advanced .NET Programming I 6th Lecture
Collections 24: Collections Programming C# © 2003 DevelopMentor, Inc.
CS313D: Advanced Programming Language
Collection types/Anders Børjesson
Visual C# "Whidbey": Language Enhancements
Lecture 10: Collections.
Visual C# 2005: Language Enhancements
Lesson 6. Types Equality and Identity. Collections.
DEV321 Visual C# 2005: Language Enhancements
Getting Ready for Visual Studio 2005
Fundaments of Game Design
Basic Collections.
鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所
Defining Interfaces using C#
Advanced .NET Programming I 13th Lecture
Advanced .NET Programming I 5th Lecture
C# Language & .NET Platform 10th Lecture
Advanced .NET Programming I 7th Lecture
Advanced .NET Programming I 3rd Lecture
Advanced .NET Programming I 4th Lecture
C# Language & .NET Platform 11th Lecture
Advanced .NET Programming I 6th Lecture
C# Language & .NET Platform 9th Lecture
C# Language & .NET Platform 4th Lecture
C# Language & .NET Platform 12th Lecture
Chengyu Sun California State University, Los Angeles
Visual C# 2005: Language Enhancements
Presentation transcript:

Advanced .NET Programming I 2nd 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)

Iterator Methods Characteristics of an interator method class MyClass { string first = "first"; string second = "second"; string third = "third"; ... public IEnumerator<string> GetEnumerator() { yield return first; yield return second; yield return third; } Characteristics of an interator method has the signature public IEnumerator<T> GetEnumerator statement body contains at least one yield statement MyClass x = new MyClass(); ... foreach (string s in x) Console.Write(s + " "); // prints "first second third " returns a sequence of values foreach loop traverses this sequence How does an iterator method work? Note MyClass need not implement IEnumerable!

What Happens Behind the Scene? returns an object of the following class public IEnumerator<int> GetEnumerator() { try { ... } finally { } class _Enumerator1 : IEnumerator<int> { int Current { get {...} } bool MoveNext() {...} void Dispose() {...} } is translated into foreach (int x in list) Console.WriteLine(x); IEnumerator<int> _e = list.GetEnumerator(); try { while (_e.MoveNext()) Console.WriteLine(_e.Current); } finally { if (_e != null) _e.Dispose(); } MoveNext runs to the next yield statement Dispose executes a possibly existing finally block in the iterator method

Implementation class Stack<T>: IEnumerable<T> { T[] items; int count; public void Push(T item) { } public T Pop() { public IEnumerator<T> GetEnumerator() { for (int i = count - 1; i >= 0; --i) { yield return items[i]; } } class Stack<T>: IEnumerable<T> { ... public IEnumerator<T> GetEnumerator() { return new __Enumerator1(this); } class __Enumerator1: IEnumerator<T>, IEnumerator { int __state; T __current; Stack<T> __this; int i; ... public bool MoveNext() { switch (__state) { case 1: goto __state1; case 2: goto __state2; } i = __this.count - 1; __loop: if (i < 0) goto __state2; __current = __this.items[i]; __state = 1; return true; __state1: --i; goto __loop; __state2: __state = 2; return false; } } }

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();

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<int> GetEnumerator() { if (left != null) { foreach (int x in left) { yield return x; yield return value; if (right != null) { foreach (int x in right) { class Program { static void Main(string[] args) { Node root = new Node( new Node( new Node(2), new Node(7), 5 ), 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();

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<int> GetEnumerator() { if (left != null) { foreach (int x in left) { yield return x; yield return value; if (right != null) { foreach (int x in right) { class Program { static void Main(string[] args) { Node root = new Node( new Node( new Node(2), new Node(7), 5 ), 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!

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

Interface ICollection : IEnumerable Interface ICollection<T> : IEnumerable, IEnumerable<T> Basic interface for collections: New in ICollection<T>: 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;

Interface IList : ICollection, IEnumerable Interface IList<T> : ICollection<T>, IEnumerable, IEnumerable<T> 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); … }

Interface IDictionary Interface IDictionary<T, U> 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<T, U> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, ... { public struct DictionaryEntry { public DictionaryEntry (object key, object value); public object Key {get;}; public object Value {get;}; }

Namespace System.Collections.Generic Classes List<T> SortedList<T, U> Dictionary<T, U> SortedDictionary<T, U> Stack<T> Queue<T> LinkedList<T> HashSet<T> SortedSet<T> 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<T> (new in .NET 2.0) implemented as a hash table in an array + provides quick set operations (new in .NET 3.5 + implements ISet<T> in 4.0) implemented as a red-black tree (new in .NET 4.0) Interfaces ICollection<T> IList<T> IDictionary<T, U> ISet<T> IEnumerable<T> IEnumerator<T> IComparable<T> IComparer<T> IEquatable<T> IEqualityComparer<T> (new in .NET 4.0)

Type Variance

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