共用方式為


Enumerable.Single<TSource> Method (IEnumerable<TSource>)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

Namespace:  System.Linq
Assembly:  System.Core (in System.Core.dll)

Syntax

'Declaration
<ExtensionAttribute> _
Public Shared Function Single(Of TSource) ( _
    source As IEnumerable(Of TSource) _
) As TSource
public static TSource Single<TSource>(
    this IEnumerable<TSource> source
)

Type Parameters

  • TSource
    The type of the elements of source.

Parameters

Return Value

Type: TSource
The single element of the input sequence.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerable<TSource>. When you use instance method syntax to call this method, omit the first parameter.

Exceptions

Exception Condition
ArgumentNullException

source is nulla null reference (Nothing in Visual Basic).

InvalidOperationException

The input sequence contains more than one element.

-or-

The input sequence is empty.

Remarks

The Single<TSource>(IEnumerable<TSource>) method throws an exception if the input sequence is empty. To instead return nulla null reference (Nothing in Visual Basic) when the input sequence is empty, use SingleOrDefault.

Examples

The following code example demonstrates how to use Single<TSource>(IEnumerable<TSource>) to select the only element of an array.

' Create an array that contains one item.
Dim fruits1() As String = {"orange"}

' Get the single item in the array.
Dim result As String = fruits1.Single()

' Display the result.
outputBlock.Text &= "First query: " & result & vbCrLf
      string[] fruits1 = { "orange" };

      string fruit1 = fruits1.Single();

      outputBlock.Text += fruit1 + "\n";

      /*
       This code produces the following output:

       orange
      */

The following code example demonstrates that Single<TSource>(IEnumerable<TSource>) throws an exception when the sequence does not contain exactly one element.

      ' Create an array that contains two items.
      Dim fruits2() As String = {"orange", "apple"}

      result = String.Empty

      ' Try to get the 'single' item in the array.
      Try
         result = fruits2.Single()
      Catch ex As System.InvalidOperationException
         result = "The collection does not contain exactly one element."
      End Try

      ' Display the result.
      outputBlock.Text &= "Second query: " & result & vbCrLf

      ' This code produces the following output:
      '
      ' First query: orange
      ' Second query: The collection does not contain exactly one element.

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

      try
      {
         fruit2 = fruits2.Single();
      }
      catch (System.InvalidOperationException)
      {
         outputBlock.Text += "The collection does not contain exactly one element." + "\n";
      }

      outputBlock.Text += fruit2 + "\n";

      /*
       This code produces the following output:

       The collection does not contain exactly one element.
      */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.