Technical blog and topic .Net With Kendo UI

Explain what is the difference between Skip() and SkipWhile() extension method?

 

  • Skip() : It will take an integer argument and from the given IEnumerable it skips the top n numbers
 static void Main()
    {
        //
        // Array that is IEnumerable for demonstration.
        //
        int[] array = { 1, 3, 5, 7, 9, 11 };
        //
        // Get collection of all elements except first two.
        //
        var items1 = array.Skip(2);
        foreach (var value in items1)
        {
            Console.WriteLine(value);
        }
         
    }
OutPut: 5,7,9,11
  • SkipWhile (): It will continue to skip the elements as far as the input condition is true. It will return all remaining elements if the condition is false
 static void Main()
    {
        int[] array = { 1, 3, 5, 10, 20 };
        var result = array.SkipWhile(element => element < 10);
        foreach (int value in result)
        {
            Console.WriteLine(value);
        }
    }

Output 10,20

Leave a comment