Time for action – leaving the first few elements

Follow the given steps:

  1. Copy the following code snippet as a query to LINQPad:
    int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,13 };
    var first4 = numbers.Take(4);
  2. Run the query. You will get the following output:
    Time for action – leaving the first few elements

What just happened?

Numbers is an int array, int in C# is an alias for System.Int32 type in CTS. Thus, the query numbers.Take(4); returns an IEnumerable<System.Int32> that has first four elements.

One good thing about Take() is that it doesn't throw an exception if the argument number is out of range (say negative, or more than the number of elements in the collection). ...

Get .NET 4.0 Generics now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.