Lesson 7. Events, Delegates, Generics.

Slides:



Advertisements
Similar presentations
Introduction to Web Application
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
CS  C++ Function Pointers  C# Method References  Groundwork for Lambda Functions.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Writing Object Oriented Software with C#. C# and OOP C# is designed for the.NET Framework  The.NET Framework is Object Oriented In C#  Your access to.
2.3 Cool features in C# academy.zariba.com 1. Lecture Content 1.Extension Methods 2.Anonymous Types 3.Delegates 4.Action and Func 5.Events 6.Lambda Expressions.
Object-Oriented Programming in Visual Basic.NET. Overview Defining Classes Creating and Destroying Objects Inheritance Interfaces Working with Classes.
Understanding Events and Exceptions Lesson 3. Objective Domain Matrix Skills/ConceptsMTA Exam Objectives Understand events and event handling Understand.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
Java2C# Antonio Cisternino Part II. Outline Array types Enum types Value types  differences from classes  boxing and unboxing Delegate types  Base.
Programming Pillars Introduction to Object- Oriented Programming.
CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
Delegates and Events Callback Functionality and Event-Driven Programming Svetlin Nakov Technical Trainer Software University
ECE122 Feb. 22, Any question on Vehicle sample code?
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Module 8: Delegates and Events. Overview Delegates Multicast Delegates Events When to Use Delegates, Events, and Interfaces.
CIS162AD Inheritance Part 3 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Abstract Classes and.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
FEN 2014UCN Teknologi/act2learn1 Higher order functions Observer Pattern Delegates Events Visitor Pattern Lambdas and closures Lambdas in libraries.
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.
Introduction to Object-Oriented Programming Lesson 2.
1.NETDelegates & eventsNOEA / PQC Delegates & events Observer pattern Delegates –Semantics –Cil – kode –Anvendelse Events.
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.
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 –
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Introduction to.NET Florin Olariu “Alexandru Ioan Cuza”, University of Iai Department of Computer Science.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Advanced Programming in Java
Advanced Programming in Java
C# - OOP TTU IT College , Autumn SEMESTER Andres käver.
Delegates and Events Svetlin Nakov Telerik Corporation
2.7 Inheritance Types of inheritance
Module 5: Common Type System
Delegates and Events 14: Delegates and Events
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2005
Chapter Eleven Handling Events.
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.
Structs.
.NET and .NET Core: Languages, Cloud, Mobile and AI
Functional Programming with Java
Interfaces.
Lecture 23 Polymorphism Richard Gesick.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Module 4: Implementing Object-Oriented Programming Techniques in C#
C# Event Processing Model
AVG 24th 2015 ADVANCED c# - part 1.
6 Delegate and Lambda Expressions
Lecture 22 Inheritance Richard Gesick.
Delegates & Events 1.
Object Oriented Practices
Advanced Programming in Java
Functional interface.
Programming in C# Lesson 5. Exceptions..
Advanced Programming in Java
CIS 199 Final Review.
DELEGATES AND EVENT MODELING
5. 3 Coding with Denotations
C++ Polymorphism Reference and pointer implicit type casting
Abstract Data Types Abstraction is to distill a system to its most fundamental parts. Applying the abstraction paradigm to the design of data structures.
Chengyu Sun California State University, Los Angeles
Events, Delegates, and Lambdas
Generics, Lambdas and Reflection
Threads and concurrency / Safety
Presentation transcript:

Lesson 7. Events, Delegates, Generics. Programming in C# Lesson 7. Events, Delegates, Generics.

Delegates

What is a delegate? A delegate is a type safe pointer to a method (or a chain of methods) All delegates are classes that inherited from MulticastDelegate class Delegates are useful in callback scenarios Delegates are used in APM (Asynchronous Programming Model) Delegates are the base of events

Declaring a delegate delegate keyword Name Return value type Parameters (number and type)

Using a delegate

Using a delegate as a callback method

Using a delegate as a callback method

Delegates chaining

Delegates chaining

Using delegates in APM

Anonymous Methods and Lambda Expressions

Events Events provide a way for an object to be notified about something that has happened in another object

Designing a Type That Exposes an Event Define a type that will hold any additional information that should be sent to subscribers Define the event member (based on existing or custom delegate) Define a method responsible for raising the event Call the method responsible for raising the event from the appropriate place

Defining a type that will hold additional information

Defining the event member

Defining a method responsible for raising the event

Calling the method responsible for raising the event

Subscribing/Unsubscribing to events

Method GetInvocationList Method GetInvocationList allows us to iterate manually through the list of subscribers and handle them correspondingly:

EventHandler and EventHandler<TEventArgs> delegates In order to avoid the necessity to create custom delegates each time we need to expose an event, these standard delegates should be used:

Generics

What are generics? Generics provide another form of code reuse: algorithm reuse Generics allow us to encapsulate an algorithm within a class, interface or method without specifying the data type(s) that class or method works on Generics provide type safety Generics provide better performance (especially for collection data structures) as they prevent the necessity of boxing/unboxing and type casting There are generic classes, interfaces, methods and delegates

A generic class sample

Operator default Operator default specifies the default value of the type parameter. This will be null for reference types and zero for value types

Constraints on Type Parameters where T: struct – The type argument must be a value type where T: class – The type argument must be a reference type where T: new() – The type argument must have a public parameterless constructor where T: <base class name> – The type argument must be or derive from the specified base class where T: <interface name> – The type argument must be or implement the specified interface

Generic Class with Constraints Sample

Generic Method with Constraints Sample