CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
public class Animal
{
public string Name { get; set; }
}
public class Cat : Animal { }
public class Dog : Animal { }
public class Program
{
public static void Main()
{
Console.WriteLine(GetFilteredAnimals("Pe", GetCats));
}
private static List<Cat> GetCats()
{
return new List<Cat>() { new Cat { Name = "Sphinx" }, new Cat { Name = "Persian" } };
}
private static List<Dog> GetDogs()
{
return new List<Dog>() { new Dog { Name = "Bulldog"}, new Dog { Name = "Dalmatian" } };
}
private static List<Animal> GetFilteredAnimals(string f, Func<List<Animal>> method)
{
var animals = method();
return animals.Where(a => a.Name.StartsWith(f)).ToList<Animal>();
}
}