Csharp/CSharp Tutorial/LINQ/SkipWhile — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:18, 26 мая 2010

Second SkipWhile Prototype

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        string[] presidents = {"ant", "arding", "arrison", "Hayes", "Hoover", "ackson"};
        IEnumerable<string> items = presidents.SkipWhile((s, i) => s.Length > 4 && i < 10);
        foreach (string item in items)
            Console.WriteLine(item);
    }
}

SkipWhile Prototype

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        string[] presidents = {"Ant", "arding", "arrison", "ayes", "Hoover", "ackson"};
        IEnumerable<string> items = presidents.SkipWhile(s => s.StartsWith("A"));
        foreach (string item in items)
            Console.WriteLine(item);
    }
}

SkipWhile with condition

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass{
public static void Main() {
            int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
            
            var allButFirst3Numbers = numbers.SkipWhile(n => n % 3 != 0);
            
            Console.WriteLine("All elements starting from first element divisible by 3:");
            foreach (var n in allButFirst3Numbers) {
                Console.WriteLine(n);
            }
        }
}

SkipWhile with index

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass{
public static void Main() {
            int[] numbers = { 5, 4, 8, 6, 7, 2, 0 };
            
            var laterNumbers = numbers.SkipWhile((n, index) => n >= index);
            
            Console.WriteLine("All elements starting from first element less than its position:");
            foreach (var n in laterNumbers) {
                Console.WriteLine(n);
            }
        }
}

Use SkipWhile with index

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public class MainClass{
   public static void Main(){
       int[] numbers = { 1, 3, 5, 4};
       var query = numbers.TakeWhile(( n, index) => n >= index);
       var query2 = numbers.SkipWhile(( n, index) => n >= index);
    }
}