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

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

Версия 15:31, 26 мая 2010

OfType: select a certain type

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
    public static void Main() {
        object[] numbers = { null, 1.0, "two", 3, 4.0f, 5, "six", 7.0 };
        var doubles = numbers.OfType<double>();
        Console.WriteLine("Numbers stored as doubles:");
        foreach (var d in doubles) {
            Console.WriteLine(d);
        }
    }
}

prints all of the elements of an array that are of type double

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
    public static void Main() {
        object[] numbers = { null, 1.0, "two", 3, 4.0f, 5, "six", 7.0 };
        var doubles = numbers.OfType<double>();
        Console.WriteLine("Numbers stored as doubles:");
        foreach (var d in doubles) {
            Console.WriteLine(d);
        }
    }
}

Use Linq OfType to get value of specific type

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public class MainClass{
   public static void Main(){
            object[] sequence = {1, "Hello", 2.0};
            Console.Write(sequence.OfType<double>());
   }
}