Download presentation
Presentation is loading. Please wait.
1
Advanced .NET Programming I 3rd Lecture
Pavel Ježek 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 (
2
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
3
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);
4
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); … }
5
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;}; }
6
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 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)
7
Type Variance
8
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;
9
Sorting: IComparable and IComparer
IComparable is interface for types with order classes implementing IComparable are values types like Int32, Double, DateTime, … class Enum as base class of all enumeration types class String IComparer is interface for the realization of compare operators public interface IComparable { int CompareTo(object obj); // <0 if this < obj, 0 if this == obj, >0 if this > obj } public interface IComparable<in T> { int CompareTo(T obj); // <0 if this < obj, 0 if this == obj, >0 if this > obj public interface IComparer { int Compare(object x, object y); // <0 if x < y, 0 if x == y, >0 if x > y } public interface IComparer<in T> { int Compare(T x, T y); // <0 if x < y, 0 if x == y, >0 if x > y
10
IEnumerable and IEnumerator (1)
Anything which is enumerable is represented by interface IEnumerable IEnumerator realizes an iterator Also generic versions IEnumerable<out T> and IEnumerator<out T> Enumerator should throw an InvalidOperationException on concurrent modification! interface IEnumerable { IEnumerator GetEnumerator(); } interface IEnumerator { object Current {get;} bool MoveNext(); void Reset(); }
11
CLI Type Inheritance pointers (C#: Type *)
System.Object (C# keyword: object) interfaces (C# keyword: interface) System.String (C# keyword: string) System.Array System.Delegate arrays (C#: Type[] or Type[,]) System.MulticastDelegate System.ValueType delegates (C# keyword: delegate) user-defined classes (C# keyword: class) simple types System.Int32 (C# keyword: int) System.Double (C# keyword: double) System.Int64 (C# keyword: long) System.Enum System.Nullable (C#: Type?) user-defined structures (C# keyword: struct) System.Boolean (C# keyword: bool) enumerations (C# keyword: enum) …
12
Delegate = Method Type Declaration of a delegate type
delegate void Notifier (string sender); // ordinary method signature // with the keyword delegate Declaration of a delegate variable Notifier greetings; Assigning a method to a delegate variable void SayHello(string sender) { Console.WriteLine("Hello from " + sender); } greetings = new Notifier(SayHello); Calling a delegate variable greetings("John"); // invokes SayHello("John") => "Hello from John"
13
Assigning Different Methods
Every matching method can be assigned to a delegate variable void SayGoodBye(string sender) { Console.WriteLine("Good bye from " + sender); } greetings = new Notifier(SayGoodBye); greetings("John"); // SayGoodBye("John") => "Good bye from John" Note A delegate variable can have the value null (no method assigned). If null, a delegate variable must not be called (otherwise exception). Delegate variables are first class objects: can be stored in a data structure, passed as a parameter, etc.
14
Creating a Delegate Value
new DelegateType (obj.Method) A delegate variable stores a method and its receiver, but no parameters ! new Notifier(myObj.SayHello); obj can be this (and can be omitted) new Notifier(SayHello); Method can be static. In this case the class name must be specified instead of obj. new Notifier(MyClass.StaticSayHello); Method signature must match the signature of DelegateType - same number of parameters - same parameter types (including the return type) - same parameter kinds (ref, out, value)
15
Simplified Creation of Delegates
delegate void Printer(string s); void Foo(string s) { Console.WriteLine(s); } Printer print; print = new Printer(this.Foo); print = this.Foo; print = Foo; simplified form: delegate type is infered from the type of the left-hand side delegate double Function(double x); double Foo(double x) { return x * x; } Printer print = Foo; Function square = Foo; assigns Foo(string s) assigns Foo(double x) overloading is resolved using the type of the left-hand side
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.