C#迭代器C#基础学习

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 using System.Collections;
 8 
 9 namespace day01
10 {
11     class Class28
12     {
13         static void Main(string[] args)
14         {
15             foreach(string temp in SimpleList())
16             {
17                 Console.WriteLine(temp);
18             }
19             Console.ReadKey();
20         }
21         static IEnumerable SimpleList()
22         {
23             yield return "string 1";
24             yield return "string 2";
25             yield return "string 3";
26         }
27     }
28 }
代码二
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 using System.Collections;
 8 
 9 namespace day01
10 {
11     class Class29
12     {
13         static void Main(string[] args)
14         {
15             Cat catDictionary = new Cat();
16             catDictionary.Add("1001", new Cat("zhangsan"));
17             catDictionary.Add("1002", new Cat("lisi"));
18             catDictionary.Add("1031", new Cat("wangwu"));
19 
20             foreach(Animal temp in catDictionary)
21             {
22                 Console.WriteLine("{0} {1}", temp.ToString(), temp.Name);
23             }
24             Console.ReadKey();
25         }
26     }
27     public abstract class Animal : DictionaryBase
28     {
29         protected string name;
30         public string Name { get; set; }
31         public Animal()
32         {
33             Name = "default";
34         }
35         public Animal(string name)
36         {
37             Name = name;
38         }
39         public abstract void Run();
40         public abstract void Sleep();
41         public override string ToString()
42         {
43             return "name: " + Name;
44         }
45         public void Add(string newId, Animal animal)
46         {
47             Dictionary.Add(newId, animal);
48         }
49         public void Remove(string newId)
50         {
51             Dictionary.Remove(newId);
52         }
53         public Animal this[string newId]
54         {
55             get
56             {
57                 return (Animal)Dictionary[newId];
58             }
59             set
60             {
61                 Dictionary[newId] = value;
62             }
63         }
64         //添加迭代器
65         public new IEnumerator GetEnumerator()
66         {
67             foreach(object temp in Dictionary.Values)
68             {
69                 yield return (Animal)temp;
70             }
71         }
72     }
73     public class Cat : Animal
74     {
75         //这个构造器该怎么理解?
76         public Cat()
77         {
78 
79         }
80         public Cat(string name) : base(name)
81         {
82 
83         }
84         public override void Run()
85         {
86             Console.WriteLine("animal name: {0} is running", Name);
87         }
88 
89         public override void Sleep()
90         {
91             Console.WriteLine("animal name: {0} is sleeping", Name);
92         }
93     }
94 }

 

 

posted @ 2019-04-08 17:05  littlelittleprince  阅读(180)  评论(0编辑  收藏  举报