Advanced .NET Programming I 3rd 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.
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.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th & 8 th Lecture Pavel Ježek.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
Windows Programming Using C# Arrays, Collections.
Object Composition Interfaces Collections Covariance Object class Programming using C# LECTURE 10.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
Delegates Programming in C# Delegates CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
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.
Evolution of.NET Collections (C#) Chen Dong
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.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
Advanced C#, part I Niels Hallenberg IT University of Copenhagen BAAAP – Spring 2009.
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.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 6 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 8 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 9 th Lecture Pavel Ježek
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.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek
- This slide is intentionally left blank - Some of the slides are based on University of Linz.NET presentations. © University of Linz, Institute for System.
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.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
C# Collections & Generics
Module 5: Programming with C#. Overview Using Arrays Using Collections Using Interfaces Using Exception Handling Using Delegates and Events.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 3 rd Lecture Pavel Ježek
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
Advanced .NET Programming I 2nd Lecture
Advanced .NET Programming I 3nd Lecture
Multidimensional Arrays
Module 5: Common Type System
Advanced .NET Programming I 4th Lecture
Iterators and Comparators
Chapter 5: Programming with C#
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
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
Visual C# 2005: Language Enhancements
Lesson 6. Types Equality and Identity. Collections.
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
- This slide is intentionally left blank -
Advanced .NET Programming I 4th Lecture
C# Language & .NET Platform 11th Lecture
Advanced .NET Programming I 6th Lecture
C# Language & .NET Platform 3rd Lecture
C# Language & .NET Platform 9th Lecture
C# Language & .NET Platform 12th Lecture
Visual C# 2005: Language Enhancements
Presentation transcript:

Advanced .NET Programming I 3rd 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)

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;

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

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

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) …

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"

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.

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)

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