40
Generic Delegate (泛型委派) Titan, Jonathan 2013.06.10

Generic Delegate

Embed Size (px)

Citation preview

Page 1: Generic Delegate

Generic Delegate (泛型委派)

Titan, Jonathan2013.06.10

Page 2: Generic Delegate

Delegate 回顧......

Page 3: Generic Delegate

上次我們談到......

■ Delegate 與使用 Delegate 4 步驟

Page 4: Generic Delegate

典型的 Delegate 使用情境

Page 5: Generic Delegate

Generic Delegate (泛型委派)

Page 6: Generic Delegate

我們先看看以下的程式碼...

Page 7: Generic Delegate
Page 8: Generic Delegate

要如何簡化程式碼呢?

Page 9: Generic Delegate

使用 Generic Method!

Page 10: Generic Delegate

如果我們要撰寫 Delegate 呢?

Page 11: Generic Delegate
Page 12: Generic Delegate

如何將 Delegate 泛型化?

Page 13: Generic Delegate

將 delegate 宣告成泛型

Page 14: Generic Delegate

如何再做進一步的簡化呢?

Page 15: Generic Delegate

.NET 常見的 Generic Delegate Type

Page 16: Generic Delegate

Func<T1, T2 , TResult>

Page 17: Generic Delegate

Func 不需要再宣告 delegate type

回傳型別

參數型別

Page 18: Generic Delegate
Page 19: Generic Delegate

Func<T>

Page 20: Generic Delegate

舉例

Page 21: Generic Delegate

IEnumerable<T>.Select()

滿足 Func<T> 的方法

delegate type 完整的宣告

Page 22: Generic Delegate

Recap■ Func<T1, T2, TResult>

■ Func 一定有回傳值

■ Func 的signature TResult 固定在最後一個

Page 23: Generic Delegate

Action<T>

Page 24: Generic Delegate
Page 25: Generic Delegate

Action<T1, T2>

Page 26: Generic Delegate
Page 27: Generic Delegate

舉例

Page 28: Generic Delegate

List<T>.Foreach

滿足 Action<T> 的方法

delegate type 完整宣告

Page 29: Generic Delegate

Action

Page 30: Generic Delegate
Page 31: Generic Delegate

Action

滿足 Action 的方法

delegate完整的宣告

Page 32: Generic Delegate

Action<T> VS. Func<T>

Page 33: Generic Delegate

■ Action<T>

Recap

■ Action VS. Action<T>

■ Action VS. Func<T>

■ Func<T> VS. Action<T>

Page 34: Generic Delegate

Predicate<T>

Page 35: Generic Delegate
Page 36: Generic Delegate

舉例

Page 37: Generic Delegate

IEnumerable<T>.Where()

滿足Predicate 的方法

delegate type 完整宣告

Page 38: Generic Delegate

Recap■ Predicate<T>

■ Predicate 輸入型別是T , 回傳是bool

■ Predicate<T> VS. Func<T> VS. Action<T>

Page 39: Generic Delegate
Page 40: Generic Delegate

Thank you!