C# Enumerable Select(IEnumerable, Func)

Description

Projects each element of a sequence into a new form.

Syntax


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

Parameters

  • TSource - The type of the elements of source.
  • TResult - The type of the value returned by selector.
  • source - A sequence of values to invoke a transform function on.
  • selector - A transform function to apply to each element.

Example

The following code example demonstrates how to use Select to project over a sequence of values.


/*from  ww  w  .  j  a  v  a2s.co m*/
using System;
using System.Linq;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  
   IEnumerable<int> squares = Enumerable.Range(1, 10).Select(x => x * x);

   foreach (int num in squares)
   {
       Console.WriteLine(num);
   }

  }
}
       

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable