本文整理汇总了C#中Animal.ToList方法的典型用法代码示例。如果您正苦于以下问题:C# Animal.ToList方法的具体用法?C# Animal.ToList怎么用?C# Animal.ToList使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Animal
的用法示例。
在下文中一共展示了Animal.ToList方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main()
{
var animals = new Animal[]
{
new Dog("Sharo", 5),
new Frog("Frogy", 10),
new Cat("Masha", 11),
new TomCat("Tom", 2, Gender.Male),
new Kitten("Kitty", 3, Gender.Female)
};
animals.ToList().ForEach(Console.WriteLine);
//var animalsBygroups = animals.GroupBy(GetAnimalKind,
// (g, a) => new { kind = g, averagAge = a.Average(animal => animal.Age) });
var animalsBygroups = animals.GroupBy(GetAnimalKind,
(g, a) => new { kind = g, averagAge = a.Average(animal => animal.Age) });
foreach (var animalGroup in animalsBygroups)
{
Console.WriteLine("The average age of {0}s is {1:f2}.", animalGroup.kind, animalGroup.averagAge);
}
}
示例2: Main
static void Main()
{
var animals = new Animal[]
{
new Dog("Spot", 2, Genter.Male),
new Cat("Pisana", 1, Genter.Femail),
new Frog("Jabcho", 1, Genter.Male),
new Kitten("Kity", 5),
new Tomcat("Tom", 4)
};
animals.ToList().ForEach(Console.WriteLine);
Console.WriteLine();
var animalsBygroups = animals.GroupBy(GetAnimalKind,
(g, a) => new { kind = g, averagAge = a.Average(animal => animal.Age) });
foreach (var animalGroup in animalsBygroups)
{
Console.WriteLine("The average age of {0}s is {1:f2}.", animalGroup.kind, animalGroup.averagAge);
}
}