46
Delegate (委派) from C# 1.0 to C# 3.0 Wayne, Jonathan 2013.05.27

Delegate (委派) Introduction

Embed Size (px)

Citation preview

Page 1: Delegate (委派) Introduction

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

Wayne, Jonathan

2013.05.27

Page 2: Delegate (委派) Introduction

What is a Delegate?

Page 3: Delegate (委派) Introduction

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

var X 3

0x3344 Age=3Name="Bill"

var Y

Value Type

Reference Type

Page 4: Delegate (委派) Introduction

var Z

MyFunc1()

MyFunc2()

Invocation List

Delegate Instance

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

Page 5: Delegate (委派) Introduction

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

Page 6: Delegate (委派) Introduction

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

Page 7: Delegate (委派) Introduction

Delegate 分為兩部份:

1. Delegate type2. Delegate instance

Page 8: Delegate (委派) Introduction

Delegate Type 宣告

delegate bool Validation (int id, double sum);

delegate 關鍵字

delegate 參照的方法的回傳型別

delegate type 名稱

delegate 參照的方法的參數列表

Page 9: Delegate (委派) Introduction

最典型的 Delegate 寫法

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

1. 宣告 Delegate Type

3. 建立 Delegate Instance

4. 叫用 Delegate Instance

Page 10: Delegate (委派) Introduction

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

C# 版本 新特性

1.0

2.0 匿名方法 (Anonymous Method)

3.0 Lambda Expression

Page 11: Delegate (委派) Introduction

Delegate coding style - C# 1.0

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

1. 宣告 Delegate Type

3. 建立 Delegate Instance

4. 叫用 Delegate Instance

Page 12: Delegate (委派) Introduction

Delegate coding style - C# 1.0 (Cont.)

Instance Method 也可以

Invoke() 方法可以省略

Page 13: Delegate (委派) Introduction

Practice!

Page 14: Delegate (委派) Introduction

請問以下用法是對的嗎?

Page 15: Delegate (委派) Introduction

請問以下用法是對的嗎?

Page 16: Delegate (委派) Introduction

Recap

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

Page 17: Delegate (委派) Introduction

1.0 語法上的缺點?

太過囉嗦!

Page 18: Delegate (委派) Introduction

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

Page 19: Delegate (委派) Introduction

Anonymous Method 宣告

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

delegate 關鍵字

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

Page 20: Delegate (委派) Introduction

Delegate coding style - C# 2.0

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

Page 21: Delegate (委派) Introduction

Delegate coding style - C# 2.0

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

1.0 的寫法

Page 22: Delegate (委派) Introduction

Anonymous Method 帶來的新語言

特性 - Closure (閉包)

Page 23: Delegate (委派) Introduction

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

Page 24: Delegate (委派) Introduction

Closure 範例

形成一個 Closure

Page 25: Delegate (委派) Introduction

2.0 vs. 1.0

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

Page 26: Delegate (委派) Introduction

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

Page 27: Delegate (委派) Introduction

Lambda Expression

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

Page 28: Delegate (委派) Introduction

Lambda Calculus 寫法

λx → x * x

λx.λy → x * y

Page 29: Delegate (委派) Introduction

C# 下的 Lambda Expression 寫法

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

(x, y) => x * y

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

Page 30: Delegate (委派) Introduction

Delegate coding style - C# 3.0

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

Page 31: Delegate (委派) Introduction

Delegate coding style - C# 3.0

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

Page 32: Delegate (委派) Introduction

從 Anonymous Method 到 Lambda Expression 的推衍

寫法轉換為 Lambda Expression

單行 Statement 可省略大括號

參數型別可由 Compiler 推論

單一參數不需括號

Page 33: Delegate (委派) Introduction

3.0 vs. 2.0

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

Page 34: Delegate (委派) Introduction

Recap

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

Page 35: Delegate (委派) Introduction

TWO more things...

Page 36: Delegate (委派) Introduction

C# Event (事件)

Page 37: Delegate (委派) Introduction

Delegate 是事件的基礎

Page 38: Delegate (委派) Introduction

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

Page 39: Delegate (委派) Introduction

使用 event 關鍵字宣告事件

Page 40: Delegate (委派) Introduction

使用事件

Page 41: Delegate (委派) Introduction

Delegate in LINQ

Page 42: Delegate (委派) Introduction

List<T>.Exists()

內建泛型 Delegate

Delegate 完整宣告

Page 43: Delegate (委派) Introduction

List<T>.Where()

Delegate 完整宣告

內建泛型 Delegate

Page 44: Delegate (委派) Introduction

Recap

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

Page 45: Delegate (委派) Introduction

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#

Page 46: Delegate (委派) Introduction

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?