C#入门学习之集合篇

主要有几种:

1 System.Array类(一个简单的数组)

2 System.Collections.ArrayList类

  先看段代码:

View Code
1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4 using System.Text;
5 using System.Collections;
6 namespace 使用集合
7 {
8 public abstract class Animal
9 {
10 protected string name;
11 public Animal(string name)
12 {
13 this.name = name;
14 }
15 public void Feed()
16 {
17 Console.WriteLine("{0}动物已经喂养过", name);
18 }
19 public string Name
20 {
21 get { return name; }
22 set { name = value; }
23 }
24 }
25 public class Chicken : Animal
26 {
27 public void LayEgg()
28 {
29 Console.WriteLine("{0}小鸡已经下蛋了", name);
30
31 }
32 public Chicken(string Name):base(Name)
33 {
34
35 }
36 }
37 public class Cow : Animal
38 {
39 public Cow(string Name)
40 : base(Name)
41 {
42
43 }
44 public void Milk()
45 {
46 Console.WriteLine("{0}大牛被挤奶了", name);
47 }
48 }
49
50 class Program
51 {
52 static void Main(string[] args)
53 {
54 Animal[] AnimalArray=new Animal[2];
55 AnimalArray[0] = new Chicken("黑鸡");
56 AnimalArray[1] = new Cow("红牛");
57 foreach (Animal a in AnimalArray)
58 {
59 Console.WriteLine("该动物是{0}", a.Name);
60 a.Feed();
61 }
62 Console.WriteLine("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
63
64 ((Chicken)AnimalArray[0]).LayEgg();
65 ((Cow)AnimalArray[1]).Milk();
66 Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
67
68 ArrayList AnimalList = new ArrayList();
69 AnimalList.Add(new Cow("红牛"));
70 Chicken chicken = new Chicken("黑鸡");
71 AnimalList.Add(chicken);
72 foreach (Animal a in AnimalList)
73 {
74 Console.WriteLine("该动物是:{0}", a.Name);
75 a.Feed();
76 }
77 ((Cow)AnimalList[0]).Milk();
78 ((Chicken)AnimalList[1]).LayEgg();
79 Console.ReadLine();
80 }
81 }
82 }

  3 System.Collection.CollectionBase类

     这个抽象类提供了集合类的许多实现方式,即把自定义的类当成一个集合,拥有集合的添加项目和删除项目的功能(这里是添加或删除类),该类实现了

IEnumerable、ICollection和IList接口。再看一段代码:

View Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Collections;
6 namespace 使用集合
7 {
8 public abstract class Animal
9 {
10 protected string name;
11 public Animal(string name)
12 {
13 this.name = name;
14 }
15 public void Feed()
16 {
17 Console.WriteLine("{0}动物已经喂养过", name);
18 }
19 public string Name
20 {
21 get { return name; }
22 set { name = value; }
23 }
24 }
25 public class Chicken : Animal
26 {
27 public void LayEgg()
28 {
29 Console.WriteLine("{0}小鸡已经下蛋了", name);
30
31 }
32 public Chicken(string Name):base(Name)
33 {
34
35 }
36 }
37 public class Cow : Animal
38 {
39 public Cow(string Name)
40 : base(Name)
41 {
42
43 }
44 public void Milk()
45 {
46 Console.WriteLine("{0}大牛被挤奶了", name);
47 }
48 }
49
50 public class Animals : CollectionBase
51 {
52 public Animals()
53 {
54
55 }
56 public void Add(Animal animal)
57 {
58 List.Add(animal);
59 }
60 public void Remove(Animal animal)
61 {
62 List.Remove(animal);
63 }
64 public Animal this[int index]
65 {
66 get { return this[index]; }
67 set { this[index] = value; }
68 }
69
70 }
71 class Program
72 {
73 static void Main(string[] args)
74 {
75 Animals AminalCollection = new Animals();
76 AminalCollection.Add(new Cow("红牛"));
77 AminalCollection.Add(new Chicken("黑鸡"));
78 foreach (Animal a in AminalCollection)
79 {
80 Console.WriteLine("该动物是:{0}", a.Name);
81 a.Feed();
82 }
83 Console.ReadLine();
84 }
85 }
86 }

这里用到了索引符,它是一种特殊类型的属性,提供类似与数组的查找和定位功能

View Code
1 public Animal this[int index]
2 {
3 get { return this[index]; }
4 set { this[index] = value; }
5 }

   4 关键字值集合和IDictonary

    基类是DictionaryBase,使用关键值(如字符串)来进行索引,该基类也实现了IEnumerable和ICollection接口,提供了对任何集合都相同的集合处理功能

View Code
1 public class Animals :DictionaryBase
2 {
3 public Animals()
4 {
5
6 }
7 public void Add(string ID,Animal animal)
8 {
9 Dictionary.Add(ID,animal);
10 }
11 public void Remove( string ID)
12 {
13 Dictionary.Remove(ID);
14 }
15 public Animal this[string ID]
16 {
17 get { return this[ID]; }
18 set { this[ID] = value; }
19 }
20
21 }
22 class Program
23 {
24 static void Main(string[] args)
25 {
26 Animals AnimalDictionary = new Animals();
27 Cow myCow = new Cow("红牛");
28 Chicken myChicken = new Chicken("黑鸡");
29 AnimalDictionary.Add("红牛", myCow);
30 AnimalDictionary.Add("黑鸡", myChicken);
31 foreach (DictionaryEntry a in AnimalDictionary)
32 {
33 ((Animal)a.Value).Feed();
34 }
35 Console.ReadLine();
36 }
37 }

   5 迭代器

     迭代器是可以返回相同类型的值的有序序列的一段代码。迭代器可用作方法、运算符或 get 访问器的代码体,

     迭代器代码使用 yield return 语句依次返回每个元素。yield break 将终止迭代。

     迭代器的返回类型必须为 IEnumerable、IEnumerator、IEnumerable<T> 或 IEnumerator<T>。

       ① 如果要迭代一个类的话,可使用方法GetEnumerator(),其返回值是IEnumerator;

       ②如果要迭代一个类成员,例如一个方法,则使用IEnumerable;

        

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace 车库例子
{
class Program
{
public class Car
{
protected string name;
protected int speed;
public string Name
{
get { return name; }
}
public Car(string name, int speed)
{
this.name = name;
this.speed = speed;

}
}
public class Garage
{
private Car[] CarLot;
public Garage()
{
CarLot
= new Car[3];
CarLot[
0] = new Car("宾利", 100);
CarLot[
1] = new Car("猛犸", 80);
CarLot[
2] = new Car("法拉利", 120);
}
public IEnumerator GetEnumerator()
{
foreach (Car a in CarLot)
{
yield return a;
}
}
public IEnumerable Simperlist()
{
yield return "好车";
yield return "坏车";
yield return "不好不坏";
}
}
static void Main(string[] args)
{
Garage g
= new Garage();

foreach (Car c in g)
{
Console.Write(
"这是一辆:{0}\n", c.Name);
}
foreach (string item in g.Simperlist())
{
Console.WriteLine(
"该车是一辆{0}", item);
}
Console.ReadLine();
}
}
}


      

posted @ 2011-03-19 11:07  师士  阅读(386)  评论(0编辑  收藏  举报