System.Linq.Enumerable.Select Method

Projects each element of a sequence into a new form.

public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, TResult> selector)

Parameters

Name Type Description
source IEnumerable<TSource> An IEnumerable<TSource> whose elements to project.
selector Func<TSource, TResult> or Func<TSource, int, TResult> A transform function to apply to each element.

Return Value

An IEnumerable<TResult> whose elements are each the result of invoking the transform function on each element of the source sequence.

Remarks

The Select extension method is used to perform a projection operation on a sequence. It transforms each element of the input sequence into a new form defined by the provided selector function. This is a fundamental operation in LINQ for shaping data.

There are two overloads for the Select method:

  • The first overload takes a selector function that only receives the element itself (Func<TSource, TResult>).
  • The second overload takes a selector function that receives both the element and its zero-based index in the source sequence (Func<TSource, int, TResult>).

The transformation is lazy; that is, the transformation is not performed until the elements of the resulting sequence are enumerated.

Exceptions

  • ArgumentNullException: source is null.
  • ArgumentNullException: selector is null.

Example

Using Select with element only


using System;
using System.Collections.Generic;
using System.Linq;

public class Example
{
    public static void Main(string[] args)
    {
        List numbers = new List<int> { 1, 2, 3, 4, 5 };

        // Project each number to its square
        var squares = numbers.Select(n => n * n);

        Console.WriteLine("Squares:");
        foreach (var square in squares)
        {
            Console.Write(square + " "); // Output: 1 4 9 16 25
        }
        Console.WriteLine();

        // Project each number to a string representation
        var stringNumbers = numbers.Select(n => $"Number: {n}");
        
        Console.WriteLine("String representations:");
        foreach (var sNum in stringNumbers)
        {
            Console.WriteLine(sNum); // Output: Number: 1, Number: 2, ...
        }
    }
}
                

Using Select with element and index


using System;
using System.Collections.Generic;
using System.Linq;

public class ExampleWithIndex
{
    public static void Main(string[] args)
    {
        List<string> fruits = new List<string> { "apple", "banana", "cherry" };

        // Project each fruit with its index
        var indexedFruits = fruits.Select((fruit, index) => $"{index}: {fruit}");

        Console.WriteLine("Indexed fruits:");
        foreach (var item in indexedFruits)
        {
            Console.WriteLine(item); // Output: 0: apple, 1: banana, 2: cherry
        }
    }
}