CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek

Slides:



Advertisements
Similar presentations
Generics Programming in C# Generics CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Advertisements

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 6 th Lecture Pavel Ježek
Static Members, Structures, Enumerations, Generic Classes, Namespaces Learning & Development Team Telerik Software Academy.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
 2006 Pearson Education, Inc. All rights reserved Generics Many slides modified by Prof. L. Lilien (even many without an explicit message). Slides.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
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++
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
 2006 Pearson Education, Inc. All rights reserved Generics.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
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 2012 UCN Technology: Computer Science1 C# - Introduction Language Fundamentals in Brief.
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)
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
Introduction to C# C# is - elegant, type-safe, object oriented language enabling to build applications that run on the.NET framework - types of applications.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
C#.Net Development Version 1.0. Overview Nullable Datatype Description ? HasValue Lifted Conversions null coalescing operator ?? Partial Classes Copyright.
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.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Understanding Data Types and Collections Lesson 2.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
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.
C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension.
Peyman Dodangeh Sharif University of Technology Spring 2014.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
ITF11006.NET Generics. Generics - Sample Recent Generics - Characteristics Performance Type Safety Binary Code Reuse Code Bloat Naming Guidelines.
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
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 9 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
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 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th Lecture Pavel Ježek
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 New Features in C# 2.0 Generic Types Iterators Simplified Delegates Anonymous Methods Partial Types Various © University of Linz, Institute for System.
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
Sixth Lecture ArrayList Abstract Class and Interface
Advanced .NET Programming I 3nd Lecture
Module 5: Common Type System
Methods Attributes Method Modifiers ‘static’
Advanced .NET Programming I 5th Lecture
C# Language & .NET Platform 10th Lecture
- This slide is intentionally left blank -
Advanced .NET Programming I 3rd Lecture
C# Language & .NET Platform 11th Lecture
C# Language & .NET Platform 3rd Lecture
C# Language & .NET Platform 9th Lecture
C# Language & .NET Platform 12th Lecture
Presentation transcript:

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th 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 (

Extension Methods Declaration: public static partial class Extensions { public static int ToInt32(this string s) { return Int32.Parse(s); } } Usage: string s = "1234"; int i = s.ToInt32(); // Same as Extensions.ToInt32(s) Instance methods take precedence over extension methods Extension methods imported in inner namespace declarations take precedence over extension methods imported in outer namespace declarations

Generic Methods

Null Values in Generic Types/Methods 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 nullable types x == null does a comparison for value types x == null returns false

Overview of Constraints ConstraintDescription where T: struct The type argument must be a value type. Any value type except Nullable can be specified. where T : class The type argument must be a reference type, including any class, interface, delegate, or array type. where T : new() The type argument must have a public parameterless constructor. When used in conjunction with other constraints, the new() must be specified last. where T : BaseClassName The type argument must be or derive from the specified base class. where T : InterfaceName The type argument must be or implement the specified interface. Multiple interface constraints can be specified. The constraining interface can also be generic. where T : U The type argument supplied for T must be or derive from the type argument supplied for U. This is called a naked type constraint.

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"}

Extension Methods Declaration: public static partial class Extensions { public static int ToInt32(this string s) { return Int32.Parse(s); } } Usage: string s = "1234"; int i = s.ToInt32(); // Same as Extensions.ToInt32(s) Instance methods take precedence over extension methods Extension methods imported in inner namespace declarations take precedence over extension methods imported in outer namespace declarations

Extension Methods (2) Declaration: public static partial class Extensions { public static T[] Slice (this T[] source, int index, int count) { if (index < 0 || count < 0 || source.Length – index < count) throw new ArgumentException(); T[] result = new T[count]; Array.Copy(source, index, result, 0, count); return result; } } Usage: int[] digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int[] a = digits.Slice(4, 3); // Same as Extensions.Slice(digits, 4, 3)

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 Type casts needed buffer.Put(3);// boxing imposes run-time costs int x = (int) buffer.Get();// type cast (unboxing) 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

Generic Class Buffer class Buffer { private Element[] data; public Buffer(int size) {...} public void Put(Element x) {...} public Element Get() {...} } placeholder typegeneric type works also for structs and interfaces placeholder type Element can be used like a normal type Usage 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 typ cast needed! Benefits homogeneous data structure with compile-time type checking efficient (no boxing, no type casts)

CLI Type Inheritance System.Object (C# keyword: object ) user-defined classes (C# keyword: class ) delegates (C# keyword: delegate ) pointers (C#: Type * ) System.Delegate System.MulticastDelegate System.ValueType System.Enum System.Array arrays (C#: Type[] or Type[,] ) System.String (C# keyword: string ) interfaces (C# keyword: interface ) user-defined structures (C# keyword: struct ) enumerations (C# keyword: enum ) System.Int32 (C# keyword: int ) System.Int64 (C# keyword: long ) System.Double (C# keyword: double ) System.Boolean (C# keyword: bool ) … simple types System.Nullable (C#: Type? )

Generics in Java Templates in C++

What Happens Behind the Scene (in C#/.NET)? class Buffer {...} Compiler generates CIL code for class Buffer with a placeholder for Element. Buffer a = new Buffer (); At run time the CLR generates a new class Buffer in which Element is replaced with int. Buffer b = new Buffer (); Uses existing Buffer. Buffer c = new Buffer (); At run time the CLR generates a new class Buffer in which Element is replaced with float. Instantiation with value types Buffer a = new Buffer (); At run time the CLR generates a new “class” impl. “ Buffer ” which can work with all reference types. Buffer b = new Buffer (); Uses existing Buffer. Instantiation with reference types Buffer b = new Buffer (); Uses existing Buffer code, but CLR generates new “description”, i.e. Method Table, for Buffer class.

Generic Buffer in ILDASM

Multiple Placeholder Types 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); C++ allows also placeholders for constants, C# does not

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

Assignment Compatibility class A {...} class B :A {...} class C :A {...} A B C... A a1 = new B (); A a2 = new C (); B... C... Assigning T to a non-generic base class class A {...} class B :A {...} class C :A {...} A a1 = new B (); A a2 = new C (); Assigning T to a generic base class A B C... A a3 = new B (); illegal ok, if corresponding placeholders are replaced by the same type

Overriding Methods class MyBuffer: Buffer {... public override void Put(int x) {...} } Methods inherited from an instantiated generic class The following is illegal (it is not allowed to inherit a placeholder) class MyBuffer: Buffer {... public override void Put(Element x) {...} } 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

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.Name); // => Buffer’1[System.Int32] Reflection yields also the concrete types substituted for the placeholders!

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

Multiple Constraints class OrderedBuffer where Element: MyClass where Priority: IComparable, 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

Constructor Constraint For creating objects of a generic types class Stack where E: Exception, new() { T[] data =...; int top = -1; public void Push(T x) { if (top >= data.Length) 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);

Overview of Constraints ConstraintDescription where T: struct The type argument must be a value type. Any value type except Nullable can be specified. where T : class The type argument must be a reference type, including any class, interface, delegate, or array type. where T : new() The type argument must have a public parameterless constructor. When used in conjunction with other constraints, the new() must be specified last. where T : BaseClassName The type argument must be or derive from the specified base class. where T : InterfaceName The type argument must be or implement the specified interface. Multiple interface constraints can be specified. The constraining interface can also be generic. where T : U The type argument supplied for T must be or derive from the type argument supplied for U. This is called a naked type constraint.

Trick: How to declare a tree of chars using Dictionary ?

Trick: How to declare a “recursive” type? If something like: Dictionary >> treeRoot; is needed …

Trick: How to declare a “recursive” type? If something like: Dictionary >> treeRoot; is needed, it can be declared as: class Node : Dictionary { // Disadvantage: // All Dictionary constructors other than Dictionary () // need to be explicitly recreated in Node class (with at least with an empty body): public Node(xxx) : base(xxx) {} public Node(yyy) : base(yyy) {}... }