.NET and .NET Core: Languages, Cloud, Mobile and AI

Slides:



Advertisements
Similar presentations
Introduction to Web Application
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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++
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Delegates and Events Tom Roeder CS fa. Motivation – Function Pointers Treat functions as first-class objects eg. Scheme: (map myfunc somelist)
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Eric Vogel Software Developer A.J. Boggs & Company.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.
Introduction to C# C# is - elegant, type-safe, object oriented language enabling to build applications that run on the.NET framework - types of applications.
Delegates Programming in C# Delegates CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Sample Application Multi Layered Architecture (n-tier): –Graphical User Interface (GUI): Forms, components, controls The Visual Designer in Visual Studio.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers.
Lambda Expressions Version 1.0
Module 8: Delegates and Events. Overview Delegates Multicast Delegates Events When to Use Delegates, Events, and Interfaces.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
FEN 2014UCN Teknologi/act2learn1 Higher order functions Observer Pattern Delegates Events Visitor Pattern Lambdas and closures Lambdas in libraries.
 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.
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.
Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Object-Oriented Programming: Inheritance and Polymorphism.
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.
V 1.0 OE-NIK HP 1 Advanced Programming Delegates, Events.
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 –
REC [ ] How to implement CAMERA RECORDING for USB WEBCAM or IP CAMERA in C#.NET SOURCE CODE: ! Welcome to this presentation that explains.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
Building Web Applications with Microsoft ASP
C# for C++ Programmers 1.
Java Programming: Guided Learning with Early Objects
Lambda Expressions Version 1.1
C# - OOP TTU IT College , Autumn SEMESTER Andres käver.
Advanced .NET Programming I 3nd Lecture
Ch 10- Advanced Object-Oriented Programming Features
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
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
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.
Delegates/ Anders Børjesson
Generics, Lambdas, Reflections
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
.NET and .NET Core 9. Towards Higher Order Pan Wuming 2017.
Introduction to Classes
C# Event Processing Model
AVG 24th 2015 ADVANCED c# - part 1.
6 Delegate and Lambda Expressions
1.2 Key Concepts and Walkthroughs
Delegates & Events 1.
Lesson 7. Events, Delegates, Generics.
Java Programming Course
CIS 199 Final Review.
5. 3 Coding with Denotations
IT College 2016, Andres käver
ITE “A” GROUP 2 ENCAPSULATION.
Chengyu Sun California State University, Los Angeles
Events, Delegates, and Lambdas
Generics, Lambdas and Reflection
Presentation transcript:

.NET and .NET Core: Languages, Cloud, Mobile and AI 5. Generic Delegate and Lambda Pan Wuming 2018

Topics Coding with Denotations Delegates Generic to Simplify Delegate Declaration The Action and Func generic delegates Covariance and Contravariance in Generic Lambda Expressions

Coding with Denotations Denoting Collection of Names Denoting Behavior Group Referring to Behaviors Enumerations Interfaces Delegates

Delegates Delegate types are derived from the Delegate class in the .NET Framework. Delegate types are sealed. The instantiated delegate can be passed as a parameter, or assigned to a property. The instantiated delegate can wrap an instance method or a static method

<Grid> <Button Content="Show" Name="ShowButton" HorizontalAlignment="Left" Height="123" Margin="52,53,0,0" VerticalAlignment="Top" Width="193" FontSize="20"/> <Button Content="Delegat to label" Name="ToLabelButton" HorizontalAlignment="Left" Margin="52,271,0,0" VerticalAlignment="Top" Width="247" Height="52" FontSize="20"/> <Button Content="Delegat to ToMessageBox" Name="ToMessageBox" HorizontalAlignment="Left" Margin="400,271,0,0" VerticalAlignment="Top" Width="315" Height="52" FontSize="20"/> <Label Content="Label" Name="ShowLabel" HorizontalAlignment="Left" Margin="345,44,0,0" VerticalAlignment="Top" Height="169" Width="384" FontSize="20"/> </Grid>

public delegate void TextShow(string s); public partial class MainWindow : Window { TextShow MyShow; public MainWindow() InitializeComponent(); ShowButton.Click = ShowButtonClick; ToLabelButton.Click = ToLabelButtonClick; ToMessageBox.Click = ToMessageBoxClick; }

public void Show(string s) { ShowLabel.Content = s; } //public event RoutedEventHandler Click; //public delegate void RoutedEventHandler(object sender, RoutedEventArgs e);

public void ShowButtonClick(object sender, RoutedEventArgs e) { MyShow("Use ShowButtonClick Method!"); } public void ToMessageBoxClick(object sender, RoutedEventArgs e) MyShow = (s) => MessageBox.Show(s); public void ToLabelButtonClick(object sender, RoutedEventArgs e) //?

Multicasting A delegate can call more than one method when invoked Delegates with more than one method in their invocation list derive from MulticastDelegate MulticastDelegate is a subclass of System.Delegate. To add an extra method to the delegate's list of methods To remove a method from the invocation list

… d1 = obj.Method1; d1 += obj.Method2; 1 2 obj.Method1 _target obj _methodPtr MethodClass.Method1 _prev null 1 _target obj _methodPtr MethodClass.Method2 _prev _target obj _methodPtr MethodClass.Method1 _prev null obj.Method2 2

<Grid> <Button Content="Show" Name="ShowButton" HorizontalAlignment="Left" Height="123" Margin="52,53,0,0" VerticalAlignment="Top" Width="193" FontSize="20"/> <Button Content="Delegat to label" Name="ToLabelButton" HorizontalAlignment="Left" Margin="52,271,0,0" VerticalAlignment="Top" Width="247" Height="52" FontSize="20"/> <Button Content="Delegat to ToMessageBox" Name="ToMessageBox" HorizontalAlignment="Left" Margin="400,271,0,0" VerticalAlignment="Top" Width="315" Height="52" FontSize="20"/> <Label Content="Label" Name="ShowLabel" HorizontalAlignment="Left" Margin="345,44,0,0" VerticalAlignment="Top" Height="169" Width="384" FontSize="20"/> <Button Content="Remove to label" Name="RemoveToLabel" HorizontalAlignment="Left" Margin="52,338,0,0" VerticalAlignment="Top" Width="247" Height="52" FontSize="20"/> <Button Content="Remove to ToMessageBox" Name="RemoveToMessageBox" HorizontalAlignment="Left" Margin="400,338,0,0" VerticalAlignment="Top" Width="315" Height="52" FontSize="20"/> </Grid>

public delegate void DelTextShow(string s); public partial class MainWindow : Window { public DelTextShow MyShow; DelTextShow L; DelTextShow M;

public MainWindow() { InitializeComponent(); ShowButton.Click += ShowButtonClick; ToLabelButton.Click += ToLabelButtonClick; ToMessageBox.Click += ToMessageBoxClick; L = ShowToLable; M = (s) => MessageBox.Show(s); RemoveToLabel.Click += RemoveToLabelClick; RemoveToMessageBox.Click += RemoveToMessageBoxClick; }

public void ShowToLable(string s) { ShowLabel.Content = s; } //public event RoutedEventHandler Click; //public delegate void RoutedEventHandler(object sender, RoutedEventArgs e);

public void ShowButtonClick(object sender, RoutedEventArgs e) { try MyShow("Use ShowButtonClick Method!"); } catch ShowLabel.Content = "Label"; } } public void ToMessageBoxClick(object sender, RoutedEventArgs e) MyShow += M; public void ToLabelButtonClick(object sender, RoutedEventArgs e) MyShow += L;

public void RemoveToLabelClick(object sender, RoutedEventArgs e) { MyShow -= L; } public void RemoveToMessageBoxClick(object sender, RoutedEventArgs e) MyShow -= M;

Generic to Simplify Delegate Declaration Problem: It does not necessary to assign different name to delegates Generics as "code template“ to define delegates in advance in class library Generics The runtime knows what type of data structure you are using and can store it in memory more efficiently. List<int> ListGeneric = new List<int> { 5, 9, 1, 4 };

public delegate void TextShow(string s); public delegate void DelTextShow(string s); public partial class AClass { public DelTextShow MyShow; public TextShow MyShowNew; public AClass() MyShow = (s) => MessageBox.Show(s); MyShowNew = (s) => MessageBox.Show(s); }

The Action and Func generic delegates A delegate can define its own type parameters. In and out Generic Modifier public delegate void Action<in T1, in T2>(T1 arg1,T2 arg2); public delegate TResult Func<in T1, out TResult>(T1 arg1);

public delegate void DelTextShow(string s); public partial class MainWindow : Window { public DelTextShow MyShow; DelTextShow L; DelTextShow M; public partial class MainWindow2 : Window { Action<string> MyShow; Action<string> L; Action<string> M;

The Action Generic Delegates in .NET Framework Class Library Description Action Encapsulates a method that has no parameters and does not return a value. Action< T> Encapsulates a method that has a single parameter and does not return a value. Action< T1, T2> Encapsulates a method that has two parameters and does not return a value. ... Action< T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> Encapsulates a method that has 15 parameters and does not return a value. Action< T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> Encapsulates a method that has 16 parameters and does not return a value.

The Func Generic Delegates in .NET Framework Class Library Description Func< TResult> Encapsulates a method that has no parameters and returns a value of the type specified by the TResult parameter. Func< T, TResult> Encapsulates a method that has one parameter and returns a value of the type specified by the TResult parameter. Func< T1, T2, TResult> Encapsulates a method that has two parameters and returns a value of the type specified by the TResult parameter. ... Func< T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, TResult> Encapsulates a method that has 16 parameters and returns a value of the type specified by the TResult parameter.

Covariance and Contravariance Covariance: Enables you to use a more specific type than originally specified. Contravariance: Enables you to use a more generic (less derived) type than originally specified. Invariance: Means that you can use only the type originally specified The in keyword specifies that the type parameter is contravariant. The out keyword specifies that the type parameter is covariant.

Contravariance class GenericDelegate { public GenericDelegate() Action<BaseClass> b = (target) => target.Write(); }; Action<DerivedClass> d = b; d(new DerivedClass()); Action<DerivedClass> e = (target) => target.DerivedClassWrite(); e(new DerivedClass()); //Action<BaseClass> f = e; } class BaseClass { public void Write() Console.WriteLine("BaseClassWrite"); } class DerivedClass : BaseClass public void DerivedClassWrite() Console.WriteLine("DerivedClassWrite"); 语法错误。

Actual passing and assigning direction is contravariant ! 课堂演练: 将前述代码中 Action 改为 Func,修改代码,以使能够运行。 Actual passing and assigning direction is contravariant !

Lambda Expressions Anonymous Methods The lambda operator => Specifying input parameters (if any) on the left side of => Putting the expression or statement block on the other side Lambdas can refer to outer variables

public void MethodA(int input) { int j = 0; Func<bool> del = () => { j = 10; return j > input; }; Func<bool> del1 = () => j > input; Func<int,bool> del2 = (x) => { return x == j; }; MethodB(() => j > input); } public void MethodB(Func<bool> delIn) { }

Calling lambda expression will change the values of variables! 在C# interactive 中执行如下代码 > int j = 0; . Func<bool> del = () => { j = 10; return j > 6; }; . del(); . Console.WriteLine(j.ToString()); 10 Calling lambda expression will change the values of variables!

> class ClassB . { . . public Func<bool> MethodA(int k) . { . int j = 0; . Func<bool> del = () => {return j > k; }; . return del; . } . public void MethodB(Func<bool> delB) . Console.WriteLine(delB().ToString()); . } . var c = new ClassB(); . c.MethodB(c.MethodA(5)); False

> class ClassB . { . . public Func<bool> MethodA(int k) . { . int j = 0; . Func<bool> del = () => {return j > k; }; . j = 10; . return del; . } . public void MethodB(Func<bool> delB) . Console.WriteLine(delB().ToString()); . } . var c = new ClassB(); . c.MethodB(c.MethodA(5)); True

Summarizing the Applications of Delegates Callback Function Event members in a Type Replace simple Interface implementation