or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing
xxxxxxxxxx
LINQ基礎 - 委派與Lambda
前言
為了要對於每一筆資料做特定的處理 , LinQ 方法常會在走訪資料集合的時候 , 透過執行委派來得到期望的結果. 而為求方便與簡潔 , LinQ 常使用 Lambda 來指定委派. 所以 Lambda 對於 LinQ 來說 , 非常重要.
委派
委派的繼承圖
MulticastDelegate
委派的使用方式
使用委派的過程如下
由上面的步驟 , 或許可將委派想成有三個層面
以下為指派實作細節(邏輯端)給委派的三種方式.
C#1.0 具名函式
將已宣告的方法指派給委派.
範例
C#2.0 匿名方法 delegate 關鍵字
格式 : delegate (arguments) { statements }
在 C# 2.0 中引入匿名方法後 , 就不必一定要使用某一個執行個體方法或靜態方法 , 來指定委派變數. 可直接透過匿名方法來定義委派要執行的內容.
範例
C#3.0 Lambda
格式 : (arguments) => expression | { statements }
比較 Lambda 與匿名方法的差異
參考此資料 , 微軟也建議我們開始使用 Lambda 取代匿名方法
範例
委派的多重特性
由上述參考可知 , 委派有一個清單儲存多個方法實體 , 並且再呼叫委派時 , 依序呼叫這些方法.
範例
輸出結果
GetInvocationList
若是委派具有回傳值 , 並需要個別取得其每一個方法的結果 , 可使用 GetInvocationList ()
範例
輸出
11
9
10
委派參數與回傳值型別變異性
委派與介面的選擇
使用委派
使用介面
結論
想要建立一個符合委派結構的實體 , 從一開始 C#1.0 只能透過指定一個具名函式 , 到 C#2.0 可以使用 delegate 關鍵字以建立匿名方法來定義匿名函式 , 到 C#3.0 可以使用更簡易的 Lambda 語法來定義匿名函式 透過 Lambda , 我們不必再額外定義一個方法去作為具名函式了 , 而是可以再需要呼叫委派時 , 馬上透過 Lambda 語法 , 即時的實體化委派並呼叫.
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →以下簡易地實作 LinQ 的 Where 方法來作為此篇的結論
輸出結果
5
8
7
Thank you!
You can find me on
若有謬誤 , 煩請告知 , 新手發帖請多包涵
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →