英語で読む

次の方法で共有


Enumerable.LastOrDefault メソッド

定義

シーケンスの最後の要素を返します。要素が見つからない場合は既定値を返します。

オーバーロード

LastOrDefault<TSource>(IEnumerable<TSource>)

シーケンスの最後の要素を返します。シーケンスに要素が含まれていない場合は既定値を返します。

LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

条件を満たす、シーケンスの最後の要素を返します。このような要素が見つからない場合は既定値を返します。

LastOrDefault<TSource>(IEnumerable<TSource>, TSource)

シーケンスの最後の要素を返します。シーケンスに要素が含まれない場合は、指定された既定値を返します。

LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>, TSource)

条件を満たすシーケンスの最後の要素を返します。そのような要素が見つからない場合は、指定された既定値を返します。

LastOrDefault<TSource>(IEnumerable<TSource>)

ソース:
Last.cs
ソース:
Last.cs
ソース:
Last.cs

シーケンスの最後の要素を返します。シーケンスに要素が含まれていない場合は既定値を返します。

C#
public static TSource LastOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);
C#
public static TSource? LastOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);

型パラメーター

TSource

source の要素の型。

パラメーター

source
IEnumerable<TSource>

最後の要素を返す IEnumerable<T>

戻り値

TSource

ソース シーケンスが空の場合は default (TSource)。それ以外の場合は IEnumerable<T> の最後の要素。

例外

sourcenullです。

次のコード例は、空の配列で を使用 LastOrDefault<TSource>(IEnumerable<TSource>) する方法を示しています。

C#
string[] fruits = { };
string last = fruits.LastOrDefault();
Console.WriteLine(
    String.IsNullOrEmpty(last) ? "<string is null or empty>" : last);

/*
 This code produces the following output:

 <string is null or empty>
*/

コレクションに要素が含まれていない場合、 の default(TSource) 値が使用する既定値ではない場合があります。 不要な既定値の結果を確認し、必要に応じて変更する代わりに、 メソッドを DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) 使用して、コレクションが空の場合に使用する既定値を指定できます。 次に、 を呼び出 Last<TSource>(IEnumerable<TSource>) して最後の要素を取得します。 次のコード例では、月の数値の日のコレクションが空の場合、両方の手法を使用して既定値 1 を取得します。 整数の既定値は 0 で、月のどの日にも対応していないため、既定値は 1 として指定する必要があります。 クエリの実行が完了した後、最初の結果変数で不要な既定値がチェックされます。 2 番目の結果変数は、 を使用 DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) して既定値の 1 を指定することによって取得されます。

C#
List<int> daysOfMonth = new List<int> { };

// Setting the default value to 1 after the query.
int lastDay1 = daysOfMonth.LastOrDefault();
if (lastDay1 == 0)
{
    lastDay1 = 1;
}
Console.WriteLine("The value of the lastDay1 variable is {0}", lastDay1);

// Setting the default value to 1 by using DefaultIfEmpty() in the query.
int lastDay2 = daysOfMonth.DefaultIfEmpty(1).Last();
Console.WriteLine("The value of the lastDay2 variable is {0}", lastDay2);

/*
 This code produces the following output:

 The value of the lastDay1 variable is 1
 The value of the lastDay2 variable is 1
*/

注釈

参照型と null 許容型の既定値は です null

メソッドには LastOrDefault 、既定値を指定する方法はありません。 以外 default(TSource)の既定値を指定する場合は、「例」セクションの DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) 説明に従って メソッドを使用します。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

ソース:
Last.cs
ソース:
Last.cs
ソース:
Last.cs

条件を満たす、シーケンスの最後の要素を返します。このような要素が見つからない場合は既定値を返します。

C#
public static TSource LastOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
C#
public static TSource? LastOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);

型パラメーター

TSource

source の要素の型。

パラメーター

source
IEnumerable<TSource>

返される要素が含まれる IEnumerable<T>

predicate
Func<TSource,Boolean>

各要素が条件を満たしているかどうかをテストする関数。

戻り値

TSource

シーケンスが空の場合、または述語関数でテストに合格する要素がない場合は default (TSource)。それ以外の場合は、述語関数でテストに合格する最後の要素。

例外

source または predicatenull です。

次のコード例では、述語を渡して を使用 LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) する方法を示します。 メソッドの 2 番目の呼び出しでは、条件を満たす要素がシーケンス内にありません。

C#
double[] numbers = { 49.6, 52.3, 51.0, 49.4, 50.2, 48.3 };

double last50 = numbers.LastOrDefault(n => Math.Round(n) == 50.0);

Console.WriteLine("The last number that rounds to 50 is {0}.", last50);

double last40 = numbers.LastOrDefault(n => Math.Round(n) == 40.0);

Console.WriteLine(
    "The last number that rounds to 40 is {0}.",
    last40 == 0.0 ? "<DOES NOT EXIST>" : last40.ToString());

/*
 This code produces the following output:

 The last number that rounds to 50 is 50.2.
 The last number that rounds to 40 is <DOES NOT EXIST>.
*/

注釈

参照型と null 許容型の既定値は です null

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

LastOrDefault<TSource>(IEnumerable<TSource>, TSource)

ソース:
Last.cs
ソース:
Last.cs
ソース:
Last.cs

シーケンスの最後の要素を返します。シーケンスに要素が含まれない場合は、指定された既定値を返します。

C#
public static TSource LastOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, TSource defaultValue);

型パラメーター

TSource

source の要素の型。

パラメーター

source
IEnumerable<TSource>

最後の要素を返す IEnumerable<T>

defaultValue
TSource

シーケンスが空の場合に返される既定値。

戻り値

TSource

defaultValue ソース シーケンスが空の場合は 。それ以外の場合は、 内の最後の IEnumerable<T>要素。

例外

sourcenull です。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET 6, 7, 8, 9

LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>, TSource)

ソース:
Last.cs
ソース:
Last.cs
ソース:
Last.cs

条件を満たすシーケンスの最後の要素を返します。そのような要素が見つからない場合は、指定された既定値を返します。

C#
public static TSource LastOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate, TSource defaultValue);

型パラメーター

TSource

source の要素の型。

パラメーター

source
IEnumerable<TSource>

返される要素が含まれる IEnumerable<T>

predicate
Func<TSource,Boolean>

各要素が条件を満たしているかどうかをテストする関数。

defaultValue
TSource

シーケンスが空の場合に返される既定値。

戻り値

TSource

defaultValue シーケンスが空の場合、または述語関数のテストに合格する要素がない場合は 。それ以外の場合は、述語関数でテストに合格する最後の要素。

例外

source または predicatenull です。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET 6, 7, 8, 9