1 New Features in C# 2.0 Generic Types Iterators Simplified Delegates Anonymous Methods Partial Types Various © University of Linz, Institute for System.

Slides:



Advertisements
Similar presentations
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Advertisements

Java Review Interface, Casting, Generics, Iterator.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 14 th Lecture Pavel Ježek
Generic programming in Java
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
 2006 Pearson Education, Inc. All rights reserved Generics.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th & 8 th Lecture Pavel Ježek.
Extension Methods Programming in C# Extension Methods CSE Prof. Roger Crawfis.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Advanced .NET Programming I 13th Lecture
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
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.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
Programming in Java CSCI-2220 Object Oriented Programming.
Understanding Data Types and Collections Lesson 2.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
[ISRAR ALI] Hammad Khan. The namespace keyword is used to declare a scope. Making software components reusable can result in naming collisions (two classes.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
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.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
Introduction to Object-Oriented Programming Lesson 2.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Introduction to C# By: Abir Ghattas Michel Barakat.
- This slide is intentionally left blank - Some of the slides are based on University of Linz.NET presentations. © University of Linz, Institute for System.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
1 Statements © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Methods. The Structure of a Method Essentially, a method is a block of code with a name. You can execute the code by using the method’s name. You can.
Module 5: Programming with C#. Overview Using Arrays Using Collections Using Interfaces Using Exception Handling Using Delegates and Events.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek
Modern Programming Tools And Techniques-I
Advanced .NET Programming I 2nd Lecture
Advanced .NET Programming I 3nd Lecture
Inheritance and Polymorphism
Advanced .NET Programming I 4th Lecture
Chapter 5: Programming with C#
Advanced .NET Programming I 5th Lecture
Advanced .NET Programming I 3rd Lecture
Advanced .NET Programming I 4th Lecture
Corresponds with Chapter 5
Presentation transcript:

1 New Features in C# 2.0 Generic Types Iterators Simplified Delegates Anonymous Methods Partial Types Various © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License released Oct. 2005

2 Problems Without Generic Types Assume we need a class that can work with arbitrary objects class Buffer { private object[] data; public void Put(object x) {...} public object Get() {...} } Problems Boxing and type casts needed buffer.Put(3);// boxing imposes run-time costs int x = (int)buffer.Get();// type cast imposes run-time costs One cannot statically enforce homogeneous data structures buffer.Put(3); buffer.Put(new Rectangle()); Rectangle r = (Rectangle)buffer.Get(); // can result in a run-time error! Special types IntBuffer, RectangleBuffer,... introduce redundancy

3 Generic Class Buffer class Buffer { private Element[] data; public Buffer(int size) {...} public void Put(Element x) {...} public Element Get() {...} } type parameter (placeholder for some real type)generic type works also for structs and interfaces placeholder type Element can be used like a normal type Usage (instantiation) Buffer a = new Buffer (100); a.Put(3);// accepts only int parameters; no boxing int i = a.Get();// no type cast needed! Buffer b = new Buffer (100); b.Put(new Rectangle());// accepts only Rectangle parameters Rectangle r = b.Get();// no type cast needed! Benefits enforces a homogeneous data structure with compile-time type checking efficient (no boxing, no type casts) Genericity is also available in Ada, Eiffel, C++ (templates), Java 5.0

4 Multiple Type Parameters Buffer with priorities class Buffer { private Element[] data; private Priority[] prio; public void Put(Element x, Priority prio) {...} public void Get(out Element x, out Priority prio) {...} } Usage Buffer a = new Buffer (); a.Put(100, 0); int elem, prio; a.Get(out elem, out prio); Buffer b = new Buffer (); b.Put(new Rectangle(), 0.5); Rectangle r; double prio; b.Get(out r, out prio);

5 Constraints Constraints about placeholder types are specified as base types class OrderedBuffer where Priority: IComparable { Element[] data; Priority[] prio; int lastElem;... public void Put(Element x, Priority p) { int i = lastElement; while (i >= 0 && p.CompareTo(prio[i]) > 0) {data[i+1] = data[i]; prio[i+1] = prio[i]; i--;} data[i+1] = x; prio[i+1] = p; } Allows operations on instances of placeholder types interface or base class Usage OrderedBuffer a = new OrderedBuffer (); a.Put(100, 3); parameter must implement IComparable

6 Multiple Constraints class OrderedBuffer where Element: MyClass where Priority: IComparable where Priority: ISerializable {... public void Put(Element x, Priority p) {...} public void Get(out Element x, out Priority p) {...} } Usage OrderedBuffer a = new OrderedBuffer ();... a.Put(new MySubclass(), new MyPrio(100)); must implement IComparable and ISerializable must be a subclass of MyClass

7 Constructor Constraint For creating objects of a parameter type class Stack where E: Exception, new() { T[] data =...; int top = -1; public void Push(T x) { if (top >= data.Length - 1) throw new E(); else data[++top] = x; } specifies that the placeholder E must have a parameterless constructor. class MyException: Exception { public MyException(): base("stack overflow or underflow") {} } Usage Stack stack = new Stack ();... stack.Push(3);

8 Genericity and Inheritance class Buffer : List {... public void Put(Element x) { this.Add(x); // Add is inherited from List } From which classes may a generic class be derived? from a non-generic class class T : B {...} from an instantiated generic class class T : B {...} from a generic class class T : B {...} with the same placeholder can also implement generic interfaces

9 Assignment Compatibility class A {...} class B :A {...} class C :A {...} A B C... A a1 = new B (); A a2 = new C (); B... C... Compatibiliy between T and a non-generic base class class A {...} class B :A {...} class C :A {...} A a1 = new B (); A a2 = new C (); Compatibility between T and a generic base class A B C... A a3 = new B (); illegal (but there is a solution for it in C# 4.0) ok, if corresponding placeholders are replaced by the same type

10 Overriding Methods class MyBuffer: Buffer {... public override void Put(int x) {...} } Methods inherited from an instantiated generic class Element is replaced by the concrete type int class Buffer {... public virtual void Put(Element x) {...} } class MyBuffer : Buffer {... public override void Put(Element x) {...} } Methods inherited from a generic class Element remains to be a placeholder The following is illegal (a concrete class cannot inherit from a generic class) class MyBuffer: Buffer {... public override void Put(Element x) {...} }

11 Run-time Type Checks Instantiated generic types can be used like non-generic types Buffer buf = new Buffer (20); object obj = buf; if (obj is Buffer ) buf = (Buffer ) obj; Type t = typeof(Buffer ); Console.WriteLine(t.FullName); // => Buffer'1[[System.Int32,...]] Reflection yields also the concrete types substituted for the placeholders! All this is not possible in Java, because in Java generic types exist only at compile time but not at run time

12 Generic Methods Methods that can work with arbitrary data types static void Sort (T[] a) where T: IComparable { for (int i = 0; i < a.Length-1; i++) { for (int j = i+1; j < a.Length; j++) { if (a[j].CompareTo(a[i]) < 0) { T x = a[i]; a[i] = a[j]; a[j] = x; } can sort any array as long as the array elements implement IComparable Usage int[] a = {3, 7, 2, 5, 3};... Sort (a); // a == {2, 3, 3, 5, 7} string[] s = {"one", "two", "three"};... Sort (s); // s == {"one", "three", "two"} From the method parameters the compiler can usually infer the concrete type that is to be substituted for the placeholder type; so one can simply write: Sort(a); // a == {2, 3, 3, 5, 7}Sort(s); // s == {"one", "three", "two"}

13 Generic Delegates delegate bool Match (T value); class Payment { public DateTime date; public int amount; } class Account { List payments = new List ();... public int AmountPayed(Match match) { int val = 0; foreach (Payment p in payments) if (match(p)) val += p.amount; return val; } bool After_9_11(Payment p) { return DateTime.Compare(p.date, new DateTime(2001, 11, 9)) >= 0; }... int val = account.AmountPayed(After_9_11); A method is passed, which checks for every payment, whether it matches a certain criterion

14 Null Values Setting a value to null void Foo () { T x = null;// error T y = 0;// error T z = default(T);// ok! 0, '\0', false, null } Comparing a value against null void Foo (T x) { if (x == null) { Console.WriteLine(true); } else { Console.WriteLine(false); } Foo(3);// false Foo(0);// false Foo("Hello");// false Foo (null);// true for reference types x == null does a comparison for value types x == null returns false

15 Namespace System.Collections.Generic New generic types Classes List Dictionary SortedList Stack Queue Interfaces ICollection IList IDictionary IEnumerable IEnumerator IComparable IComparer corresponds to ArrayList corresponds to Hashtable

16 What Happens Behind the Scene? class Buffer {...} Compiler generates CIL code for class Buffer with a placeholder for Element. Buffer a = new Buffer (); The JIT compiler generates a new class Buffer in which Element is replaced with int. Buffer b = new Buffer (); Reuses existing Buffer. Buffer c = new Buffer (); The JIT compiler generates a new class Buffer in which Element is replaced with float. Instantiation with value types Buffer a = new Buffer (); The JIT compiler generates a new class Buffer which can work with all reference types. Buffer b = new Buffer (); Reuses existing Buffer. Instantiation with reference types Buffer b = new Buffer (); Reuses existing Buffer.

17 Differences to Other Languages C++ template class Buffer {... Element Get(); } Buffer b1; no constraints, less type-safe placeholders can also be used for constants Java since Version 5.0 placeholders can only be instantiated with reference types placeholders are mapped to type Object generates implicit type casts (imposes tun-time costs) generics are just in the Java language and not in the VM reflection does not yield exact type information about placeholders class Buffer {... Element Get() {...} } Buffer b1;

18 New Features in C# 2.0 Generic Types Iterators Simplified Delegates Anonymous Methods Partial Types Various © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License

19 Iterators so far foreach loop can be applied to objects of classes which implement IEnumerable class MyClass: IEnumerable {... public IEnumerator GetEnumerator() { return new MyEnumerator(...); } class MyEnumerator: IEnumerator { public object Current { get {...} } public bool MoveNext() {...} public void Reset() {...} } MyClass x = new MyClass();... foreach (object obj in x)... complicated to implement!! interface IEnumerable { IEnumerator GetEnumerator(); } interface IEnumerator { object Current; bool MoveNext(); void Reset(); }

20 Iterator Methods class MyClass { string first = "first"; string second = "second"; string third = "third";... public IEnumerator GetEnumerator() { yield return first; yield return second; yield return third; } Characteristics of an iterator method 1.has the signature public IEnumerator GetEnumerator 2.statement body contains at least one yield statement MyClass x = new MyClass();... foreach (string s in x) Console.Write(s + " "); // prints "first second third" 1.returns a sequence of values 2.foreach loop traverses this sequence How does an iterator method work? Note MyClass needs not implement IEnumerable! Instead of IEnumerator it is better to use IEnumerator here (avoids a type cast) IEnumerator is in System.Collections.Generic

21 What Happens Behind the Scene? public IEnumerator GetEnumerator() { try {... } finally {... } returns an object of the following compiler-generated class class _Enumerator : IEnumerator { int Current { get {...} } bool MoveNext() {...} void Dispose() {...} } foreach (int x in list) Console.WriteLine(x); is translated into IEnumerator _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

22 yield Statement 2 kinds yield return expr; yields a value for the next iteration of the foreach loop type of expr must be compatible with - T (if IEnumerator ) - object (if IEnumerator) yield break; terminates the iteration (may only occur in iterator methods)

23 Specific Iterators class MyList { int[] data =...; public IEnumerator GetEnumerator() { for (int i = 0; i < data.Length; i++) yield return data[i]; } Standard iterator method Specific iterator method arbitrary name and parameter list result type IEnumerable or IEnumerable must contain a yield statement public IEnumerable Range(int from, int to) { if (to > data.Length) to = data.Length; for (int i = from; i < to; i++) yield return data[i]; } Specific iterator property arbitrary name result type IEnumerable or IEnumerable must contain a yield statement public IEnumerable Downwards { get { for (int i = data.Length - 1; i >= 0; i--) yield return data[i]; } MyList list = new MyList(); foreach (int x in list) Console.WriteLine(x); foreach (int x in list.Range(2, 7)) Console.WriteLine(x); foreach (int x in list.Downwards) Console.WriteLine(x);

24 How Specific Iterators are Compiled public IEnumerable Range(int from, int to) { if (to > data.Length) to = data.Length; for (int i = from; i < to; i++) yield return data[i]; } class _Enumerable : IEnumerable { IEnumerator GetEnumerator() {...} } class _Enumerator : IEnumerator { int from, to; int Current { get {...} } bool MoveNext() {...} void Dispose() {..} } returns an object of the following class this returns an object of the following class foreach (int x in list.Range(2, 7)) Console.WriteLine(x); IEnumerator _e = list.Range(2, 7).GetEnumerator(); try { while (_e.MoveNext()) Console.WriteLine(_e.Current); } finally { if (_e != null) _e.Dispose(); } is translated into

25 Example: Iterating Over a Tree class Tree { Node root = null; public void Add(int val) {...} public bool Contains(int val) {...} public IEnumerator GetEnumerator() { return root.GetEnumerator(); } class Node { public int val; public Node left, right; public Node(int x) { val = x; } }... Tree tree = new Tree();... foreach (int x in tree) Console.WriteLine(x); Usage Creates an enumerator object for every node of the tree! public IEnumerator GetEnumerator() { if (left != null) foreach (int x in left) yield return x; yield return val; if (right != null) foreach (int x in right) yield return x; }

26 New Features in C# 2.0 Generic Types Iterators Simplified Delegates Anonymous Methods Partial Types Various © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License

27 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

28 New Features in C# 2.0 Generic Types Iterators Simplified Delegates Anonymous Methods Partial Types Various © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License

29 Ordinary Delegates class C { int sum = 0; void SumUp(Node p) { sum += p.value; } void Print(Node p) { Console.WriteLine(p.value); } void Main() { List list = new List(); list.ForAll(SumUp); list.ForAll(Print); } requires the declaration of named methods (SumUp, Print,...) SumUp and Print cannot access the local variables of Main => sum must be declared as a global field delegate void Visitor(Node p); class List { Node[] data =...;... public void ForAll(Visitor visit) { for (int i = 0; i < data.Length; i++) visit(data[i]); }

30 Anonymous Methods delegate void Visitor(Node p); class C { void Main() { List list = new List(); int sum = 0; list.ForAll(delegate (Node p) { Console.WriteLine(p.value); }); list.ForAll(delegate (Node p) { sum += p.value; }); } method body is specified in-place does not require the declaration of a named method anonymous method can access Main's local variable sum both anonymous methods are of type Visitor here return terminates the anonymous method (not the enclosing method) formal parameterbody class List {... public void ForAll(Visitor visit) {... } Restrictions anonymous methods must not have formal parameters of the kind params T[] anonymous methods must not access ref or out parameters of the enclosing method

31 Further Simplification Button button = new Button(); button.Click += delegate (object sender, EventArgs arg) { Console.WriteLine("clicked"); }; Can be simplified as follows button.Click += delegate { Console.WriteLine("clicked"); }; Formal parameters can be omitted if they are not used in the method body Restriction Formal parameters can only be omitted if the delegate type does not have out parameters delegate void EventHandler (object sender, EventArgs arg);

32 Outer Variables If anonymous methods access variables of the enclosing method these variables are evacuated to the heap (capturing). delegate int Adder(); class Test { static Adder CreateAdder() { int x = 0; return delegate { x++; return x; }; } static void Main() { Adder add = CreateAdder(); Console.WriteLine(add()); } Output: x++; return x; 0 x on the heap method add 123 The x on the heap lives as long as the method object

33 New Features in C# 2.0 Generic Types Iterators Simplified Delegates Anonymous Methods Partial Types Various © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License

34 Class Consisting of Multiple Parts public partial class C { int x; public void M1(...) {...} public int M2(...) {...} } file Part1.cs public partial class C { string y; public void M3(...) {...} public void M4(...) {...} public void M5(...) {...} } file Part2.cs What are partial classes used for? One part can be machine-generated, other parts can be hand-written Should only be used in rare cases

35 New Features in C# 2.0 Generic Types Iterators Simplified Delegates Anonymous Methods Partial Types Various © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License

36 Static Classes May only have static fields and methods static class Math { public static double Sin(double x) {...} public static double Cos(double x) {...}... } Benefit shows explicitly that this class contains only static members the compiler can make sure that all members are declared as static Static classes must not be used as types