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
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
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.
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.
 2006 Pearson Education, Inc. All rights reserved Generics.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
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#
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
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
Java2C# Antonio Cisternino Part II. Outline Array types Enum types Value types  differences from classes  boxing and unboxing Delegate types  Base.
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.
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.
Understanding Data Types and Collections Lesson 2.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
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
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
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 9 th Lecture Pavel Ježek
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Introduction to C# By: Abir Ghattas Michel Barakat.
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 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
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
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 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’
CS360 Windows Programming
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Conditional Statements
Java Programming Language
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 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 (

CLI Type System All types Reference types (allocated on managed heap) PointersValue types (allocated in-place [with exceptions] ) Classes (e.g. strings) Interfaces ArraysDelegates Simple types (Int32, Int64, Double, Boolean, Char, …) Nullables Enumerations Structures User defined structures

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

Arrays One-dimensional arrays int[] a = new int[3]; int[] b = new int[] {3, 4, 5}; int[] c = {3, 4, 5}; SomeClass[] d = new SomeClass[10];// array of references SomeStruct[] e = new SomeStruct[10];// array of values (directly in the array) Multidimensional arrays (rectangular) int[,] a = new int[2, 3];// block matrix int[,] b = {{1, 2, 3}, {4, 5, 6}};// can be initialized directly int[,,] c = new int[2, 4, 2]; Multidimensional arrays (jagged) int[][] a = new int[2][]; // array of references to other arrays a[0] = new int[] {1, 2, 3}; // cannot be initialized directly a[1] = new int[] {4, 5, 6};

Multidimensional Arrays int[][] a = new int[2][]; a[0] = new int[3]; a[1] = new int[4]; int x = a[0][1]; Jagged (like in Java) a a[0] a[1] int[,] a = new int[2, 3]; int x = a[0, 1]; Rectangular (like in C/C++) a a[0][1] a[0, 1]

Multidimensional Arrays int[][] a = new int[2][]; a[0] = new int[3]; a[1] = new int[4]; int x = a[0][1]; Jagged (like in Java) a a[0] a[1] int[,] a = new int[2, 3]; int x = a[0, 1]; Rectangular (like in C/C++) a a[0][1] a[0, 1] Antipattern in current.NET (slower access)

Other Array Properties Indexes start at 0 Array length int[] a = new int[3]; Console.WriteLine(a.Length); // 3 int[][] b = new int[3][]; b[0] = new int[4]; Console.WriteLine("{0}, {1}", b.Length, b[0].Length); // 3, 4 int[,] c = new int[3, 4]; Console.WriteLine(c.Length); // 12 Console.WriteLine("{0}, {1}", c.GetLength(0), c.GetLength(1)); // 3, 4 System.Array provides some useful array operations int[] a = {7, 2, 5}; int[] b = new int[2]; Array.Copy(a, b, 2);// copies a[0..1] to b Array.Sort(b);...

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) {}... }

What is the output the following program? struct S { public int x; public int y; public override string ToString() { return string.Format("(x={0},y={1})", x, y); } class Program { static void Main(string[] args) { S[] arr = new S[1]; arr[0] = new S(); arr[0].x = 10; Console.Write(arr[0]); List list = new List (); list.Add(new S()); list[0].x = 20; Console.Write(list[0]); } OptionResult A(x=10,y=0)(x=20,y=0) B(x=10,y=0)(x=0,y=0) CIt will generate a runtime error (unhandled exception). DIt will not compile.

What is the output the following program? struct S { public int x; public int y; public override string ToString() { return string.Format("(x={0},y={1})", x, y); } class Program { static void Main(string[] args) { S[] arr = new S[1]; arr[0] = new S(); arr[0].x = 10; Console.Write(arr[0]); List list = new List (); list.Add(new S()); list[0].x = 20; Console.Write(list[0]); } OptionResult A(x=10,y=0)(x=20,y=0) B(x=10,y=0)(x=0,y=0) CIt will generate a runtime error (unhandled exception). DIt will not compile: “Cannot modify the return value of 'System.Collections.Generic.List.this[int]' because it is not a variable”

CLI Type System All types Reference types (allocated on managed heap) PointersValue types (allocated in-place [with exceptions] ) Classes (e.g. strings) Interfaces ArraysDelegates Simple types (Int32, Int64, Double, Boolean, Char, …) Nullables Enumerations Structures User defined structures

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

Interfaces