using System;
using System.Collections.Generic;
using System.Linq; namespace myMethod
{
class Animal
{
public string typeName = "";
public Animal(string typeName)
{
this.typeName = typeName;
}
} class Person : Animal
{
public Person(string typeName)
: base(typeName)
{ }
} class Cat : Animal
{
public Cat(string typeName)
: base(typeName)
{ }
} class lgs
{
static void Main()
{
List<Animal> list = GetAnimlList(); var itor = list.GetEnumerator();
while (itor.MoveNext())
{
Console.WriteLine(itor.Current.typeName);
} Console.ReadKey();
} static List<Animal> GetAnimlList()
{
IEnumerable<Animal> config = null;
config = GetAnimalConfig();
return null == config ? null : config.ToList(); //改方法为Enumerable类的一个扩展方法,所在的命名空间:System.Linq
} //注意是泛型枚举,不是List,
static IEnumerable<Animal> GetAnimalConfig()
{
yield return new Person("Type_Person");
yield return new Cat("Type_Cat");
}
}
}
主要需要注意的是行:57 - 61 、 49 - 53