C#: advanced object-oriented features

Similar documents
C#: advanced object-oriented features

Java: advanced object-oriented features

Java Fundamentals (II)

Java: introduction to object-oriented features

C#: framework overview and in-the-small features

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

Object-Oriented Programming in C# (VS 2015)

Object-Oriented Programming in C# (VS 2012)

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Introduce C# as Object Oriented programming language. Explain, tokens,

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

Introduction to Programming Using Java (98-388)

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Assertions. Assertions - Example

Object-Oriented Programming

Chapter 4 Defining Classes I

Absolute C++ Walter Savitch

02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211

INTRODUCTION TO.NET. Domain of.net D.N.A. Architecture One Tier Two Tier Three Tier N-Tier THE COMMON LANGUAGE RUNTIME (C.L.R.)

Adding Contracts to C#

Saikat Banerjee Page 1

Assertions, pre/postconditions

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Java and C# in Depth

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Whidbey Enhancements to C# Jeff Vaughan MSBuild Team July 21, 2004

MCSA Universal Windows Platform. A Success Guide to Prepare- Programming in C# edusum.com

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Universe Type System for Eiffel. Annetta Schaad

University of West Bohemia in Pilsen. Faculty of Applied Sciences. Department of Computer Science and Engineering DIPLOMA THESIS

Short Notes of CS201

C#.Net. Course Contents. Course contents VT BizTalk. No exam, but laborations

Table of Contents Preface Bare Necessities... 17

Objects and Classes. Lecture 10 of TDA 540 (Objektorienterad Programmering) Chalmers University of Technology Gothenburg University Fall 2017

CS201 - Introduction to Programming Glossary By

Introduction To C#.NET

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

DOT NET Syllabus (6 Months)

Java: framework overview and in-the-small features

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. C#: reflection

COPYRIGHTED MATERIAL. Contents. Part I: C# Fundamentals 1. Chapter 1: The.NET Framework 3. Chapter 2: Getting Started with Visual Studio

Sri Vidya College of Engineering & Technology

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Chapter 1 Getting Started

Java Object Oriented Design. CSC207 Fall 2014

C# 6.0 in a nutshell / Joseph Albahari & Ben Albahari. 6th ed. Beijin [etc.], cop Spis treści

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Lecture 10. Overriding & Casting About

PES INSTITUTE OF TECHNOLOGY

CS 231 Data Structures and Algorithms, Fall 2016

A Short Summary of Javali

C # Language Specification

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Assignment 2 - Specifications and Modeling

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CPS 506 Comparative Programming Languages. Programming Language

A Practical Approach to Programming With Assertions

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

CSE 307: Principles of Programming Languages

C++ Important Questions with Answers

D Programming Language

CSCI312 Principles of Programming Languages!

Instantiation of Template class


Glossary. For Introduction to Programming Using Python By Y. Daniel Liang

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Chapter Goals. Chapter 7 Designing Classes. Discovering Classes Actors (end in -er, -or) objects do some kinds of work for you: Discovering Classes

Module 10 Inheritance, Virtual Functions, and Polymorphism

C# Programming in the.net Framework

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

C# and Java. C# and Java are both modern object-oriented languages

VALLIAMMAI ENGINEERING COLLEGE

Problem Solving with C++

Course Hours

Chapter 5 Object-Oriented Programming

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Proceedings of the 8 th International Conference on Applied Informatics Eger, Hungary, January 27 30, Vol. 2. pp Virtuoso Virtuality

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: reflection

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Automated Fixing of Programs with Contracts

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s

Chapter 9. Subprograms

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

hwu-logo.png 1 class Rational { 2 int numerator ; int denominator ; 4 public Rational ( int numerator, int denominator ) {

CMSC 330: Organization of Programming Languages

Introduction to Inheritance

Reference Grammar Meta-notation: hfooi means foo is a nonterminal. foo (in bold font) means that foo is a terminal i.e., a token or a part of a token.

For this section, we will implement a class with only non-static features, that represents a rectangle

.Net Technologies. Components of.net Framework

Code Contracts in C#

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Programming

Another IS-A Relationship

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Transcription:

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer C#: advanced object-oriented features

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Namespaces

Namespaces Classes can be grouped in namespaces A hierarchical grouping of classes and other entities Every source file defines a global namespace possibly implicitly, if the user doesn t provide a name May affect visibility (but in general namespace assembly) Unlike Java, there need not be any connection between namespaces and directory structure The following are allowed in C# and disallowed in (the official implementation of) Java: multiple public top-level classes in the same file splitting the declaration of a class across multiple files 3

Using namespaces Namespace declaration: namespace MyNameSpace {... } Load all classes in a namespace (but not subnamespaces) with the using keyword: using System; Console.WriteLine( Hi! ); instead of: System.Console.WriteLine( Hi! ); Upon importing you can declare an alias: using MyConsole = System.Console; MyConsole.WriteLine( Hi! ); 4

Partial classes The keyword partial denotes classes, structs, and interfaces whose definition is split in multiple parts (possibly in different files) within the same namespace and assembly. Modifiers of the parts cannot conflict (e.g., public vs. private); if optional, the most general one is assumed (e.g., abstract vs. non-abstract). Possible usages: Automatic incremental code generation Separation of programmers work public partial class Employee { // in file1.cs int Salary(int year); } public partial class Employee { // in file2.cs string Role(); } 5

BCL: Base Class Library System (basic language functionality, fundamental types) System.Collections (collections of data structures) System.IO (streams and files) System.Net (networking and sockets) System.Reflection (reflection) System.Security (cryptography and management of permissions) System.Threading (multithreading) System.Windows.Forms (GUI components, nonstandard, specific to the Windows platform) 6

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Abstract classes and interfaces

Abstract classes and interfaces A class member may or may not have an implementation if it lacks an implementation, it is abstract A class whose implementation is not complete is also called abstract but even a fully implemented class can be declared abstract Interfaces are a form of fully abstract classes they enable a restricted form of multiple inheritance 8

Abstract classes An abstract class cannot be directly instantiated An abstract method cannot be directly executed If a class has an abstract method, the class itself must be abstract An abstract class cannot be sealed Useful for conceptualization and partial implementations 9

Interfaces Declared using interface instead of class Equivalent to a fully abstract class you don t use the keyword abstract in an interface A way to have some of the benefits of multiple inheritance, with little hassle (e.g., selecting implementations) A class may inherit from one or more interfaces If the class inherits from another class and some interfaces, the class must come first in the inheritance list An interface can also inherit from one or more interfaces 10

Interface use For typing, implementing an interface is essentially equivalent to extending a class: polymorphism applies All interface members are implicitly abstract and public (and non-static) But the interface itself may have restricted visibility Interfaces can have: methods, properties, events, indexers Interfaces cannot have fields This is C# s way to push programmers to have only private or protected fields What s the principle behind this? 11

Method name clash in multiple interfaces Two interfaces I1 and I2 may define two methods with the same name and signature. If a class C a extends both I1 and I2, it can provide two implementations of the method, one for each interface. This is called explicit interface implementation. If only one implicit implementation is provided, the two methods are merged (same behavior as in Java) Explicit interface implementations are also applicable when there is no name clash Methods realizing an explicit interface implementations can only be called using references of the interface s type Having both implicit and explicit interface implementations of the same method is also allowed 12

Explicit interface implementation interface I1 { int Weight(); } // in kg interface I2 { int Weight(); } // in lbs class C : I1, I2 { int I1.Weight() { return this.volume * 3; } // roughly, 1 kg = 2 lbs int I2.Weight() { return this.volume * 3 * 2; } } C c = new C(); int w = c.weight(); // compiler error! I1 i1 = (I1) c; I2 i2 = (I2) c; int kg = i1.weight(); // in kg int lbs = i2.weight(); // in lbs 13

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Delegates and events

Events and Delegates C# provides language features for event-driven programming most common application: GUI programming Delegates are object wrappers for operations similar to C/C++ function pointers, but with type safety similar to Eiffel s agents similar functionality achieved in Java with anonymous inner classes Events are signals sent by an object to communicate the occurrence of an action event publisher: object which can signal an event event subscriber: object which triggers some action when an event is signalled multicast communication applies an event can have multiple subscribers a subscriber can subscribe to multiple events 15

Delegates Delegates are object wrappers for operations They can be declared anywhere as members in a namespace, including outside any class The declaration includes a return type, a name, a list of arguments public delegate void BinaryOp (int i, int j); This is a placeholder for methods taking two integer arguments and returning none 16

Delegates After being declared, delegates can be instantiated by passing a handler to an actual method implementation The signature (and return type) of the passed method must match that of the delegate BinaryOp bop = new BinaryOp(adder.AddPrint); adder.addprint references method AddPrint of object attached to reference adder. You can attach (and remove) multiple methods to the same delegate, or attach the same method multiple times bop += new BinaryOp(multiplier.MultPrint); BinaryOp b2 = StaticMethodOfCurrent + new BinaryOp(adder.AddPrint) + new BinaryOp(adder.AddPrint); b2 -= StaticMethodOfCurrent; 17

Delegates After instantiation, a delegate can be invoked the net effect is equivalent to calling synchronously the passed method(s) bop(3, 5); // prints 3+5 and 3*5 if multiple methods are attached to the delegate, their order of execution is nondeterministic if the attached methods return a value, the call through the delegate returns the last computed value Other methods are available to control the invocation order and use multiple returned values foreach (BinaryOP b in bop.getinvocationlist()) { } b(3, 5); // single invocation 18

Events Events are signals sent by an object to communicate the occurrence of an action An event is a member of some class The declaration associates an event name to a delegate type public event BinaryOp BOPRequest; Any class that can trigger the event will have a trigger method for the event naming convention for the trigger method: OnEventName void OnBOPRequest(int i, int j) { } if (BOPRequest!= null) { BOPRequest(i,j); } 19

Events Subscribers to an event provide a handler for that event in the form of a method They register it on the event using the delegate type associated to the event BOPRequest += new BinaryOp(adder.AddPrint); BOPRequest += new BinaryOp(multiplier.MultPrint); Whenever the event is triggered, all the registered methods of the subscribers are executed synchronously OnBOPRequest(3, 5); // prints 3+5 and 3*5 Delegates provide a mechanism to decouple event generation and handling: the writer of the event class doesn t know what handlers will be attached to it 20

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Special classes and features

The String class Sequences of Unicode characters string (all lowercase) is an alias for String Immutable class: no setters Some differences w.r.t. Java: == and!= pre-defined to compare string content, not addresses Individual characters accessible with array notation Console.WriteLine( Hi! [2]); // prints:! Two formats for constant strings: quoted: escape characters are processed String s = A \ path\ c:\\mydir\\onwindows ; @-quoted: escape characters are not processed String s = @ A path c:\mydir\onwindows ; 22

Object comparison: Equals public boolean Equals(Object obj) { return (this == obj); } The default semantics compares addresses We can provide a different semantics by redefining it What kind of redefinition is appropriate (overriding or shadowing)? Implementation should be an equivalence relation Reflexive, symmetric, transitive For any non-null reference variable x it should be: x.equals(null) == false It is usually necessary to override GetHashCode() as well, because equal objects should have equal hash codes 23

Class Object: hash code public virtual int GetHashCode() The default implementation of GetHashCode() does not guarantee that different objects return different hash codes. In general, it is necessary to override GetHashCode(), so that equal objects have equal hash codes. Overriding Equals() in descendants does not guarantee to give the right semantics to GetHashCode() as well. 24

Class Object: string representation public virtual String ToString()returns a string representation of the object Tip: all descendants should override this method Tip: the result should be a concise and informative representation 25

Arrays Arrays are objects of class System.Array but with the familiar syntax to access them Operator [ ] to access components Field Length denotes the number of elements All components must a have a common type a common ancestor in the inheritance hierarchy Array components are automatically initialized to defaults Three variants Monodimensional: string[] arr; Multidimensional: string[,,] arr_3d; Jagged (array-of-arrays): string[][] aofa; aofa[i] is a reference to a mono-dimensional array 26

Array use // mono-dimensional of size 7 int[] iarray = new int[7]; // multi-dimensional of size 2x5x8 int[,,] mdarray = new int[2,5,8]; // jagged int[][] jarray = new int[2][]; // using initializers Vehicle[] v1 = {new Car(), new Truck()}; int[,] mdarray = {{1,2}, {3,4}, {5,6}}; int[][] jarray = new int[]{new int[] {0, 1}, new int[] {8,7,6}}; 27

Enumerated types Denote a finite set of named integer values enum TypeName : inttype {VALUE_1,..., VALUE_N}; inttype defaults to int if omitted Enumeration starts from 0 by default, with step 1 Can define different values: VALUE_3 = 8; Within the type system, TypeName is a class that extends class System.Enum and with N static integer values TypeName avalue = TypeName.VALUE_2; Unlike in Java, C# s enum does not define a full-fledged class with constructors, fields, etc. 28

Enumerated types (cont d) Convenient way to define a set of integer constants enum Days {Monday, Tuesday,...}; An enum declaration defines a type with limited capabilities Variable instantiation: Days d = new Days(); // d has value 0 Can refer to elements of enumeration: d = Days.Monday + 3; // d has value 0+3 Default initialization of enum s without 0 yields undefined behavior: enum Parity {odd = 1, even = 2}; Parity p = new Parity(); The default initialization of p is to the default int value 0: p = (Parity) 0; // allowed but undefined! 29

Structs Structs are a sort of lightweight classes mostly supported for continuity from C/C++ Can have fields, methods, and other features of classes Important differences between structs and full-fledged classes structs define value types: they are stack-allocated difference if passed as method arguments if constructors are present, they must have arguments can be instantiated without new (by directly setting fields) but then cannot be used until all fields are initialized can implement interfaces cannot inherit from another struct or class Tip: if you need methods and constructors, you d probably better use a class 30

Properties Properties are shorthands to define pairs of setter and getter for a field Properties are syntactic sugar to facilitate proper encapsulation A property has a name and a type For a client of the class, a property is indistinguishable from a field with the same name A property can have a setter, a getter, or both Keywords: set, get Within a setter: value refers to the value passed to the setter A property can also be static 31

Properties: example public class Employee { private int empage; public int Age { get { return empage; } set { empage = value; } } } Usage: Employee e = new Employee(); e.age = 33; // calls setter with value==33 int a = e.age; // calls getter 32

Properties: example (2) public class Employee { private int empage; public int Age { get { return empage; } set { empage = value; } } } This straightforward implementation of properties is equivalent to the default: public class Employee { } public int Age { get; set; } 33

Indexers Indexers are similar to properties, but for indexed fields typically arrays (and possibly other maps) An indexer has a type and an index argument no specific name public class ATPRanking { } private string[] list = new string[1000]; public string this[int pos] { } get { if (1 <= pos && pos <= 1000) return list[pos-1]; else return ; } set {if (1 <= pos && pos <= 1000)list[pos-1]=value;} Usage: ATPRanking r = new ATPRanking(); r[8] = Roger Federer ; string n = r[100]; // calls setter // calls getter 34

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Assertions and contracts

Contracts Contracts are specification elements embedded in the program text. They use the same syntax as Boolean expressions of the language. Here s an example with Eiffel syntax. class BankAccount balance: INTEGER deposit (amount: INTEGER) require amount > 0 // precondition do balance := balance + amount ensure balance > old balance end // postcondition invariant balance >= 0 end // class invariant 36

Contracts: preconditions The precondition of a method M specifies requirements that every call to M must satisfy. It is the caller s responsibility to ensure that the precondition is satisfied. ba: BankAccount create ba // object creation ba.deposit (120) // valid call: 120 > 0 ba.deposit (-8) // invalid call: -8 < 0 37

Contracts: postconditions The postcondition of a method M specifies conditions that hold whenever an invocation to M terminates. M s body is responsible to ensure that the postcondition is satisfied. ba: BankAccount create ba // object creation // assume balance is 20 ba.deposit (10) // postcondition ok: 30 > 20 ba.deposit (MAX_INTEGER) // postcondition violation if balance silently overflows into the negatives 38

Contracts: class invariants The class invariant of a class C constrains the states that instances of the class can take. The class invariant s semantics is a combination of the semantics of pre- and postcondition: the class invariant must hold upon object creation, right before every qualified call to public members of C, and right after every call terminates. ba: BankAccount create ba // object creation // class invariant must hold // class invariant must hold ba.deposit (10) // class invariant must hold 39

Assertions The.NET framework offers assertions: checks that can be placed anywhere in the executable code: Debug.Assert(boolean-expr) Assertion checking is enabled only in debug builds: If evaluates to true, nothing happens If evaluates to false, the run is interrupted and control returns to the debugger We can use assertions to render the semantics of contracts. 40

Code Contracts Since version 4.0, the.net framework includes full support of contracts through CodeContracts Preconditions, postconditions, class invariants Runtime checking (exception mechanism) Static checking Documentation generation CodeContracts are offered as a library rather than natively Advantage: available across the.net platform Disadvantage: verbose syntax Currently only partially supported in the Mono platform More information: http://research.microsoft.com/en-us/projects/contracts/ 41

Code Contracts: example using System.Diagnostics.Contracts; class BankAccount { int balance; void deposit (int amount) { Contract.Requires (amount > 0); // precondition Contract.Ensures // postcondition (balance > Contract.OldValue <int>(balance)); balance += amount; } } [ContractInvariantMethod] // class invariant void ClassInvariant() { Contract.Invariant(balance >= 0); } 42