C# Enumerable First(IEnumerable)

Description

Returns the first element of a sequence.

Syntax


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

Parameters

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

Returns

Returns the first element of a sequence.

Example

The following code example demonstrates how to use First to return the first element of an array.


/*from  w  w w . ja  va 2  s. c om*/
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  
    
            int[] numbers = { 9, 67, 12, 19 };

            int first = numbers.First();

            Console.WriteLine(first);

  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable