Csharp/C Sharp/LINQ/All

Материал из .Net Framework эксперт
Версия от 11:38, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

All with condition

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public class MainClass{
   public static void Main(){
       int[] numbers = { 2, 6, 1, 56, 102 };
       Console.Write(numbers.All(e => e % 2 == 0) ? "Yes, they are" : "No, they aren"t");
   }
}


All with Predicate to Return True

 
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        string[] presidents = {"G123456", "H", "a", "H", "over", "Jack"};
        bool all = presidents.All(s => s.Length > 5);
        Console.WriteLine(all);
    }
}


All with string length

 
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        string[] presidents = {"G", "H", "a", "H", "over", "Jack"};
        bool all = presidents.All(s => s.Length > 3);
        Console.WriteLine(all);
    }
}


uses All to determine whether an array contains only odd numbers.

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
    public static void Main() {
        int[] numbers = { 1, 11, 3, 19, 41, 65, 19 };
        bool onlyOdd = numbers.All(n => n % 2 == 1);
        Console.WriteLine("The list contains only odd numbers: {0}", onlyOdd);
    }
}