Delegate (委派) Introduction

Preview:

Citation preview

Delegate (委派)from C# 1.0 to C# 3.0

Wayne, Jonathan

2013.05.27

What is a Delegate?

一個語言機制,供我們可以建立一種特殊性質的變數

var X 3

0x3344 Age=3Name="Bill"

var Y

Value Type

Reference Type

var Z

MyFunc1()

MyFunc2()

Invocation List

Delegate Instance

這種變數指向的是「方法的參考」

其實您已經在使用 Delegate...

對以下的程式是否覺得熟悉?

Delegate 分為兩部份:

1. Delegate type2. Delegate instance

Delegate Type 宣告

delegate bool Validation (int id, double sum);

delegate 關鍵字

delegate 參照的方法的回傳型別

delegate type 名稱

delegate 參照的方法的參數列表

最典型的 Delegate 寫法

2. 宣告滿足 Delegate Type 要求的函式

1. 宣告 Delegate Type

3. 建立 Delegate Instance

4. 叫用 Delegate Instance

Delegate 從 C# 1.0 ~ 3.0語法上的演進

C# 版本 新特性

1.0

2.0 匿名方法 (Anonymous Method)

3.0 Lambda Expression

Delegate coding style - C# 1.0

2. 宣告滿足 Delegate Type 要求的函式

1. 宣告 Delegate Type

3. 建立 Delegate Instance

4. 叫用 Delegate Instance

Delegate coding style - C# 1.0 (Cont.)

Instance Method 也可以

Invoke() 方法可以省略

Practice!

請問以下用法是對的嗎?

請問以下用法是對的嗎?

Recap

■ delegate 與其他常見變數上的差異■ 使用 delegate 的 4 步驟

1.0 語法上的缺點?

太過囉嗦!

Delegate 在 C# 2.0 語法上的改變 - Anonymous Method

Anonymous Method 宣告

delegate ( 參數列 ) { 方法主體 };

delegate 關鍵字

方法的回傳值型別由 Compiler 推論

Delegate coding style - C# 2.0

原始的 2, 3 步驟,使用一行即完成

Delegate coding style - C# 2.0

原始的 2, 3 步驟,使用一行即完成

1.0 的寫法

Anonymous Method 帶來的新語言

特性 - Closure (閉包)

Closure 允許匿名方法存取外部變數

Closure 範例

形成一個 Closure

2.0 vs. 1.0

■ 引入 Anonymous Method (匿名方法)■ 引入了 Closure (閉包) 機制

Delegate 在 C# 3.0 語法上的改變 - Lambda Expression

Lambda Expression

- 概念取自於程式語言理論中的 "Lambda Calculus"

Lambda Calculus 寫法

λx → x * x

λx.λy → x * y

C# 下的 Lambda Expression 寫法

(參數1, 參數2, ...) => <單行運算式>x => x * x

(x, y) => x * y

(參數1, 參數2, ...) => {多行運算式}

Delegate coding style - C# 3.0

2、3步驟簡化為使用 Lambda Expression

Delegate coding style - C# 3.0

2、3步驟簡化為使用 Lambda Expression

從 Anonymous Method 到 Lambda Expression 的推衍

寫法轉換為 Lambda Expression

單行 Statement 可省略大括號

參數型別可由 Compiler 推論

單一參數不需括號

3.0 vs. 2.0

■ 引入 Lambda Expression■ 更加重了型別推論 (Type Inference)的使用

Recap

■ C# 在 delegate 語法上的演進■ Anonymous Method 與 Closure■ Lambda Expression 與型別推論

TWO more things...

C# Event (事件)

Delegate 是事件的基礎

.Net 內建與事件相關的 Delegate - EventHandler

使用 event 關鍵字宣告事件

使用事件

Delegate in LINQ

List<T>.Exists()

內建泛型 Delegate

Delegate 完整宣告

List<T>.Where()

Delegate 完整宣告

內建泛型 Delegate

Recap

■ Event 與 delegate 的關係■ Delegate 在 LINQ 中的使用

ReferenceMSDN 參考資料

■ delegate (C# 參考)■ event (C# 參考)■ 委派 (C# 程式設計手冊)■ 事件 (C# 程式設計手冊)■ 匿名函式 (C# 程式設計手冊)■ ASP.NET Page Life Cycle Overview

書籍參考資料

■ Learning C# 3.0: Master the fundamentals of C# 3.0 - Delegates and Events

■ C# 3.0 Cookbook, Third Edition: More than 250 solutions for C# 3.0 programmers - Delegates, Events, and Lambda Expressions

■ C# 4.0 in a Nutshell - Chapter 04 Advanced C#

Reference (Cont.)論壇、Blog 文章

■ Huan-Lin 學習筆記 - C# 筆記:重訪委派-從 C# 1.0 到 2.0 到 3.0■ Huan-Lin 學習筆記 - C# 筆記:從 Lambda 表示式到 LINQ■ In 91 - [.NET]快快樂樂學LINQ系列前哨戰-Lambda的簡介

■ 老赵点滴 - 追求编程之美 - 从.NET中委托写法的演变谈开去(上):委托与匿

名方法■ 老赵点滴 - 追求编程之美 - 从.NET中委托写法的演变谈开去(中):Lambda

表达式及其优势

■ 老赵点滴 - 追求编程之美 - 从.NET中委托写法的演变谈开去(下):性能相关

■ 老赵点滴 - 追求编程之美 - 警惕匿名方法造成的变量共享

■ 老赵点滴 - 追求编程之美 - 您善于使用匿名函数吗?

■ InfoQ 中文 - 高阶函数、委托与匿名方法

■ Stack Overflow 討論 delegate 是否為 reference type 的討論串 - Why are delegates reference types?