英語で読む

次の方法で共有


Enumerable.Single メソッド

定義

シーケンスの 1 つの特定の要素を返します。

オーバーロード

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

指定された条件を満たす、シーケンスの唯一の要素を返し、そのような要素が複数存在する場合は例外をスローします。

Single<TSource>(IEnumerable<TSource>)

シーケンスの唯一の要素を返し、シーケンス内の要素が 1 つだけでない場合は例外をスローします。

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

ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs

指定された条件を満たす、シーケンスの唯一の要素を返し、そのような要素が複数存在する場合は例外をスローします。

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

型パラメーター

TSource

source の要素の型。

パラメーター

source
IEnumerable<TSource>

1 つの要素を返す IEnumerable<T>

predicate
Func<TSource,Boolean>

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

戻り値

TSource

条件を満たす、入力シーケンスの 1 つの要素。

例外

source または predicatenull です。

predicate の条件を満たす要素はありません。

- または -

predicate の条件を満たす要素が複数あります。

- または -

ソース シーケンスが空です。

次のコード例では、 を使用 Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) して、条件を満たす配列の唯一の要素を選択する方法を示します。

C#
string[] fruits = { "apple", "banana", "mango",
                      "orange", "passionfruit", "grape" };

string fruit1 = fruits.Single(fruit => fruit.Length > 10);

Console.WriteLine(fruit1);

/*
 This code produces the following output:

 passionfruit
*/

次のコード例では、シーケンスに Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 条件を満たす要素が 1 つだけ含まれていない場合に例外をスローする方法を示します。

C#
string fruit2 = null;

try
{
    fruit2 = fruits.Single(fruit => fruit.Length > 15);
}
catch (System.InvalidOperationException)
{
    Console.WriteLine(@"The collection does not contain exactly
                    one element whose length is greater than 15.");
}

Console.WriteLine(fruit2);

// This code produces the following output:
//
// The collection does not contain exactly
// one element whose length is greater than 15.

注釈

入力シーケンスに一致する要素が含まれない場合、メソッドは Single<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 例外をスローします。 一致する要素が見つからないときに を返 null すには、 を使用します SingleOrDefault

適用対象

.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

Single<TSource>(IEnumerable<TSource>)

ソース:
Single.cs
ソース:
Single.cs
ソース:
Single.cs

シーケンスの唯一の要素を返し、シーケンス内の要素が 1 つだけでない場合は例外をスローします。

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

型パラメーター

TSource

source の要素の型。

パラメーター

source
IEnumerable<TSource>

1 つの要素を返す IEnumerable<T>

戻り値

TSource

入力シーケンスの 1 つの要素。

例外

sourcenullです。

入力シーケンスに複数の要素が含まれています。

- または -

入力シーケンスが空です。

次のコード例では、 を使用 Single<TSource>(IEnumerable<TSource>) して配列の唯一の要素を選択する方法を示します。

C#
string[] fruits1 = { "orange" };

string fruit1 = fruits1.Single();

Console.WriteLine(fruit1);

/*
 This code produces the following output:

 orange
*/

次のコード例では、シーケンスに Single<TSource>(IEnumerable<TSource>) 1 つの要素が含まれていない場合に例外をスローする方法を示します。

C#
string[] fruits2 = { "orange", "apple" };
string fruit2 = null;

try
{
    fruit2 = fruits2.Single();
}
catch (System.InvalidOperationException)
{
    Console.WriteLine("The collection does not contain exactly one element.");
}

Console.WriteLine(fruit2);

/*
 This code produces the following output:

 The collection does not contain exactly one element.
*/

注釈

メソッドは Single<TSource>(IEnumerable<TSource>) 、入力シーケンスが空の場合に例外をスローします。 代わりに、入力シーケンスが空のときに を返 null すには、 を使用 SingleOrDefaultします。

適用対象

.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