Language: C#
OfType Linq Test
class Program { static void Main(string[] args) { List<Person> people = new List<Person>(); people.Add(new Person() { FirstName = "John", LastName = "Doe"}); people.Add(new Person() { FirstName = "Jane", LastName = "Doe" }); people.Add(new Employee() { FirstName = "John", LastName = "Smith" }); // Displays 3 Console.WriteLine("Number of People In Collection: {0}", people.Count); // Displays 1 Console.WriteLine("Number of Employees In Collection: {0}", people.OfType<Employee>().Count()); Console.ReadKey(); } } public class Person { public string FirstName { get; set; } public string LastName { get; set; } } public class Employee : Person { public int EmployeeId { get; set; } }
Tags:
Comment:
Just a quick test to see if OfType<T> does a cast or just pulls straight from how the objects were put into the collection of baseclasses.
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

