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
First、Single、Last、ElementAt
LINQ 中有設計一組專門用來取出來源序列中單一項目值的擴充方法 , 也就是 First、Single、Last、ElementAt . 它們也都搭配了一組取不到資料就輸出來源資料型別預設值的方法 : FirstOrDefault、LastOrDefault、ElementAtOrDefault、SingleOrDefault. 值得注意的是 , 這八個方法都是立即執行(Immediately execution)查詢. 因此不用擔心延遲執行的問題.
First
First 算是 LinQ 取值類擴充方法中很常使用的方法 , 它會回傳來源序列中的第一個元素或符合條件的第一個元素. 若序列中不存在符合條件的元素 , 它會拋出例外. 它有兩個多載方法如下
使用時機
First 的用處
取出序列中第一個項目 、 以及第一個年紀大於七十的項目
輸出結果
(小王 , 15) (阿高 , 74)
簡單實作自己的 First
FirstOrDefault
FirstOrDefault 也是 LinQ 取值類擴充方法中常使用的方法 , 它會回傳來源序列中的第一個元素或符合條件的第一個元素. 若序列中不存在符合條件的元素 , 它會回傳該集合元素型別的預設值. 它有兩個多載方法如下
使用時機
FirstOrDefault 的用處
取出序列中第一個項目 、 第一個年紀大於七十的項目以及第一個年紀大於八十的項目( 若不存在 , 不要拋出例外 , 而是回傳預設值 )
輸出結果
(小王 , 15) (阿高 , 74) ( null , 0 )
簡單實作自己的 FirstOrDefault
Last
與 First 的用法完全相同 , 差別只在於 Last 會回傳來源序列中的最後一個元素或符合條件的最後一個元素.
使用時機
Last 的用處
取出序列中最後一個項目 、 以及最後一個年紀大於七十的項目
輸出結果
(阿高 , 74) (阿高 , 74)
簡單實作自己的 Last
LastOrDefault
與 FirstOrDefault 的用法完全相同 , 差別只在於 LastOrDefault 會回傳來源序列中的最後一個元素或符合條件的最後一個元素.
使用時機
LastOrDefault 的用處
取出序列中最後一個項目 、 最後一個年紀大於七十的項目以及最後一個年紀大於八十的項目( 若不存在 , 不要拋出例外 , 而是回傳預設值 )
輸出結果
(阿高 , 74) (阿高 , 74) ( null , 0 )
簡單實作自己的 LastOrDefault
Single
Single 會回傳來源序列中唯一的元素或唯一一個符合條件的元素. 若序列中不存在符合條件的元素或是超過一個符合條件的元素 , 它會拋出例外. 它有兩個多載方法如下
使用時機
Single 的用處
分別使用兩個多載形式去取出 唯一一個年紀大於七十的項目.
輸出結果
(阿高 , 74) (阿高 , 74)
簡單實作自己的 Single
SingleOrDefault
SingleOrDefault 會回傳來源序列中唯一的元素或唯一一個符合條件的元素.
它有兩個多載方法如下
SingleOrDefault<TSource>(thisIEnumerable<TSource>)
SingleOrDefault<TSource>(this IEnumerable<TSource>, Func<TSource,Boolean>)
使用時機
SingleOrDefault 的用處
分別使用兩個多載形式去取出 唯一一個年紀大於七十的項目.
簡單實作自己的 SingleOrDefault
總結 - First、Single、Last、FirstOrDefault、LastOrDefault、SingleOrDefault
圖片來源
ElementAt
取出序列中指定索引位置的項目. 若索引值不存在 , 則拋出例外.
使用時機
- 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 →ElementAt 的用處
取出索引位置為 1 的人
輸出結果
(老黃 , 31)
簡單實作自己的 ElementAt
ElementAtOrDefault
ElementAtOrDefault 使用上與 ElementAt 無異 , 差別只在於 index 不存在時 , 不會拋出例外 , 而是回傳型別的預設值.
使用時機
ElementAtOrDefault 的用處
取出索引位置為 1 的人
輸出結果
(老黃 , 31)
簡單實作自己的 ElementAtOrDefault
總結
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 →