generic delegate

Post on 07-Aug-2015

3.723 Views

Category:

Technology

9 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Generic Delegate (泛型委派)

Titan, Jonathan2013.06.10

Delegate 回顧......

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

■ Delegate 與使用 Delegate 4 步驟

典型的 Delegate 使用情境

Generic Delegate (泛型委派)

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

要如何簡化程式碼呢?

使用 Generic Method!

如果我們要撰寫 Delegate 呢?

如何將 Delegate 泛型化?

將 delegate 宣告成泛型

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

.NET 常見的 Generic Delegate Type

Func<T1, T2 , TResult>

Func 不需要再宣告 delegate type

回傳型別

參數型別

Func<T>

舉例

IEnumerable<T>.Select()

滿足 Func<T> 的方法

delegate type 完整的宣告

Recap■ Func<T1, T2, TResult>

■ Func 一定有回傳值

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

Action<T>

Action<T1, T2>

舉例

List<T>.Foreach

滿足 Action<T> 的方法

delegate type 完整宣告

Action

Action

滿足 Action 的方法

delegate完整的宣告

Action<T> VS. Func<T>

■ Action<T>

Recap

■ Action VS. Action<T>

■ Action VS. Func<T>

■ Func<T> VS. Action<T>

Predicate<T>

舉例

IEnumerable<T>.Where()

滿足Predicate 的方法

delegate type 完整宣告

Recap■ Predicate<T>

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

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

Thank you!

top related