|
本文章是由人工翻譯。 將指標移到文章內的文字上方即可查看原文。
|
譯文
原文
|
Enumerable.FirstOrDefault<TSource> 方法 (IEnumerable<TSource>, Func<TSource, Boolean>)
.NET Framework 4
組件: System.Core (在 System.Core.dll 中)
public static TSource FirstOrDefault<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate )
型別參數
- TSource
source 之項目的型別。
參數
- source
- 型別:System.Collections.Generic.IEnumerable<TSource>
傳回項目的 IEnumerable<T>。
- predicate
- 型別:System.Func<TSource, Boolean>
用來測試每個項目是否符合條件的函式。
傳回值
型別:TSource使用注意事項
在 Visual Basic 和 C# 中,您可以在任何 IEnumerable<TSource> 型別物件中呼叫這個方法以做為執行個體。使用執行個體方法語法呼叫這個方法時,請省略第一個參數。如需詳細資訊,請參閱擴充方法 (Visual Basic)或擴充方法 (C# 程式設計手冊)。| 例外狀況 | 條件 |
|---|---|
| ArgumentNullException |
|
string[] names = { "Hartono, Tommy", "Adams, Terry",
"Andersen, Henriette Thaulow",
"Hedlund, Magnus", "Ito, Shu" };
string firstLongName = names.FirstOrDefault(name => name.Length > 20);
Console.WriteLine("The first long name is '{0}'.", firstLongName);
string firstVeryLongName = names.FirstOrDefault(name => name.Length > 30);
Console.WriteLine(
"There is {0} name longer than 30 characters.",
string.IsNullOrEmpty(firstVeryLongName) ? "not a" : "a");
/*
This code produces the following output:
The first long name is 'Andersen, Henriette Thaulow'.
There is not a name longer than 30 characters.
*/
Windows 7, Windows Vista SP1 (含) 以後版本, Windows XP SP3, Windows Server 2008 (不支援伺服器核心), Windows Server 2008 R2 (SP1 (含) 以後版本支援伺服器核心), Windows Server 2003 SP2
.NET Framework 並不支援各種平台的所有版本。如需支援的版本的清單,請參閱.NET Framework 系統需求。