C# Enumerable Single(IEnumerable)

Description

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

Syntax


public static TSource Single<TSource>(
  this IEnumerable<TSource> source
)

Parameters

  • TSource - The type of the elements of source.
  • source - An IEnumerable to return the single element of.

Example

The following code example demonstrates how to use Single to select the only element of an array.


/*w  w w  .j  a v  a2s  .c  o m*/
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  

    string[] fruits1 = { "orange" };

    string fruit1 = fruits1.Single();

    Console.WriteLine(fruit1);

  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable