From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.

Slides:



Advertisements
Similar presentations
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
CS  C++ Function Pointers  C# Method References  Groundwork for Lambda Functions.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
CS 211 Inheritance AAA.
Programming Based on Events
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++
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
C# Event Processing Model Solving The Mystery. Agenda Introduction C# Event Processing Macro View Required Components Role of Each Component How To Create.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.
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:
Delegates Programming in C# Delegates CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Module 8: Delegates and Events. Overview Delegates Multicast Delegates Events When to Use Delegates, Events, and Interfaces.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
FEN 2014UCN Teknologi/act2learn1 Higher order functions Observer Pattern Delegates Events Visitor Pattern Lambdas and closures Lambdas in libraries.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
Introduction to Object-Oriented Programming Lesson 2.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Methods Chapter 6 - Methods.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Classes - Intermediate
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Arrays & Enum & Events. Arrays Arrays are data structures consisting of related data items of the same type. Arrays are fixed-length entities—they remain.
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 –
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Delegates and Events 14: Delegates and Events
Methods Attributes Method Modifiers ‘static’
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.
C# Event Processing Model
Corresponds with Chapter 7
6 Delegate and Lambda Expressions
Classes and Data Abstraction
Chapter 4 (part 2).
Group Status Project Status.
Delegates & Events 1.
CS2011 Introduction to Programming I Objects and Classes
CIS 199 Final Review.
Java Programming Language
C++ Object Oriented 1.
Creating and Using Classes
Events, Delegates, and Lambdas
Presentation transcript:

From C++ to C# Part 5

Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.

Delegate A delegate type represents references to methods with a particular parameter list and return type A delegate type represents references to methods with a particular parameter list and return type You can assign method references to delegate variables You can assign method references to delegate variables Delegate variables can be passed as parameters to methods Delegate variables can be passed as parameters to methods

Delegate vs Function Pointers Similar to Function pointers Similar to Function pointers But delegates are object-oriented and type safe But delegates are object-oriented and type safe

Delegate example delegate double Function(double x); delegate double Function(double x); Declares Function as a delegate type which takes in one parameter of type double and returns double. Declares Function as a delegate type which takes in one parameter of type double and returns double. Function f; Function f; Declares f as a variable of Function delegate type Declares f as a variable of Function delegate type f can reference any method which has appropriate argument signature f can reference any method which has appropriate argument signature

… Delegate example Method Apply() takes in a double array and a Function delegate type parameter Method Apply() takes in a double array and a Function delegate type parameter Apply() then invokes method referenced by Function for each element of the double array and stores the return value (for each element) in a new double array Apply() then invokes method referenced by Function for each element of the double array and stores the return value (for each element) in a new double array Finally Apply() returns the new double array Finally Apply() returns the new double array

… Delegate example static double[] Apply(double[] a, Function f) { static double[] Apply(double[] a, Function f) { double[] result = new double[a.Length]; double[] result = new double[a.Length]; for (int i = 0; i < a.Length; i++) for (int i = 0; i < a.Length; i++) result[i] = f(a[i]); result[i] = f(a[i]); return result; return result; }

… Delegate example Apply method is passed a delegate type parameter f which references appropriate methods. Apply method is passed a delegate type parameter f which references appropriate methods. f(a[i]); invokes the method referenced by delegate type parameter f f(a[i]); invokes the method referenced by delegate type parameter f

… Delegate example Method Square() has argument signature matching Function delegate type Method Square() has argument signature matching Function delegate type static double Square(double x) { static double Square(double x) { return x * x; return x * x; }

… Delegate example … So Apply() method can be called using a Function delegate variable initialized to reference method Square() So Apply() method can be called using a Function delegate variable initialized to reference method Square() double[] squares = Apply(a, double[] squares = Apply(a, new Function(Square)); new Function(Square)); Note that a is an array of double (input data) Note that a is an array of double (input data)

Delegate example code See 1.11.cs See 1.11.cs Delegate can reference either a static method or an instance method Delegate can reference either a static method or an instance method Delegate does not know or care about the class of the method it references Delegate does not know or care about the class of the method it references The method should just have the same argument signature The method should just have the same argument signature

Multi-cast Delegate A Delegate can reference multiple methods (of matching argument signature) A Delegate can reference multiple methods (of matching argument signature) See 15.3.cs See 15.3.cs Delegate variable supports =, += and -= operations Delegate variable supports =, += and -= operations

Event vs Delegate The EventHandler of.Net Framework (seen in list.cs) is defined as follows. The EventHandler of.Net Framework (seen in list.cs) is defined as follows. public delegate void EventHandler( public delegate void EventHandler( Object sender, Object sender, EventArgs e EventArgs e )

Event vs Delegate So in list.cs we could have a delegate field: So in list.cs we could have a delegate field: public EventHandler Changed; public EventHandler Changed; Instead of Instead of public event EventHandler Changed; public event EventHandler Changed; Adding references to delegate type variables is same as for event. (=, +=, -=) Adding references to delegate type variables is same as for event. (=, +=, -=) Invoking the method references is also same as for event. Invoking the method references is also same as for event.

… Event vs. Delegate “event” keyword is actually just a modifier for the delegate type “event” keyword is actually just a modifier for the delegate type It encapsulates the delegate field and allows lesser number of operations on the ‘event’ field. It encapsulates the delegate field and allows lesser number of operations on the ‘event’ field. += or -= are allowed on events += or -= are allowed on events But invoking the (public) event like Changed(…,…) is NOT ALLOWED for class users. But invoking the (public) event like Changed(…,…) is NOT ALLOWED for class users.

… Event vs. Delegate In list.cs the invocation of the event is done by the protected methods OnChanged() which is called by the public Add() method. In list.cs the invocation of the event is done by the protected methods OnChanged() which is called by the public Add() method. Thus firing the Changed event (i.e. invoking Changed(…,…)) is limited to list class and classes derived from list. Thus firing the Changed event (i.e. invoking Changed(…,…)) is limited to list class and classes derived from list. This is a significant protection from inadvertent or malicious firing of the event from class user code. This is a significant protection from inadvertent or malicious firing of the event from class user code.

… Event vs. Delegate But invoking the (public) delegate field IS ALLOWED for class users But invoking the (public) delegate field IS ALLOWED for class users And so no protection is provided against class users inadvertently or maliciously invoking the delegate method reference(s). And so no protection is provided against class users inadvertently or maliciously invoking the delegate method reference(s). There may be other differences also between Events And Delegates (e.g. thread safety) There may be other differences also between Events And Delegates (e.g. thread safety) But for our purposes this knowledge is enough. But for our purposes this knowledge is enough.

Attributes Self Study: Section 1.12 Self Study: Section 1.12

Observation10 Write your observations about the programs studied today (1.11.cs and 15.3.cs) Write your observations about the programs studied today (1.11.cs and 15.3.cs)