1 使用集合
using System;
using System.Collections;
namespace Ch11Ex01
{
class Program
{
static void Main(string[] args)
{
string str = "";
str = $"{1}+{2}={3}";
Console.WriteLine(str);
str = string.Format("{0}+{1}={2}", 1, 2, 3);
Console.WriteLine(str);
Animal[] animals = new Animal[2];
Cow cow = new Cow("花花");
cow.Milk();
cow.Feed();
animals[0] = cow;
animals[0].Feed();
animals[1] = new Chicken("波波");
animals[1].Feed();
Chicken chc = (Chicken)animals[1];
((Chicken)animals[1]).LayEgg();
chc.LayEgg();
Cow cow2 = new Cow("大牛");
Cow cow3 = new Cow("二牛");
ArrayList animalList = new ArrayList();
animalList.Add(cow);
animalList.Add(cow2);
animalList.Add(cow3);
animalList.Add(chc);
animalList.Add(cow);
animalList.AddRange(animals);
animalList.RemoveAt(animalList.Count - 1);
animalList.Remove(cow2);
Console.WriteLine();
foreach (Animal a in animalList)
{
Console.Write(a.Name+",");
}
Console.WriteLine();
}
}
}
using System;
namespace Ch11Ex01
{
public abstract class Animal
{
protected string name;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public Animal()
{
name = "无名";
}
public Animal(string name)
{
this.name = name;
}
public void Feed()
{
Console.WriteLine($"{name}已经被喂过了");
}
}
}
using System;
namespace Ch11Ex01
{
class Cow:Animal
{
public void Milk() => Console.WriteLine($"{name}挤奶");
public Cow(string newName) : base(newName) { }
}
}
using System;
namespace Ch11Ex01
{
class Chicken:Animal
{
public void LayEgg() => Console.WriteLine($"{name}下蛋");
public Chicken(string newName):base(newName){ }
}
}
- 这个示例中有两个对象集合:System.Array类 和 System.Collections.ArrayList类
- 这两个集合的对象都是Animal,而Animal类是抽象类,所以不能进行实例化。
- 通过多态性,使集合中的项成为派生于Animal类的Cow和Chicken类,从而实现了实例化。
- 集合的创建,对于简单数组而言,初始化时必须要固定数组长度。而ArrayList集合是不需要初始化其大小的。
2 创建集合
- 如何创建强类型化的集合?
- 在上面的例子的基础上增加一个Animals类
class Animals:CollectionBase
{
public void Add(Animal newAnimal) => List.Add(newAnimal);
public void Remove(Animal newAnimal) => List.Remove(newAnimal);
public Animal this[int animalIndex]
{
get
{
return (Animal)List[animalIndex];
}
set
{
List[animalIndex] = value;
}
}
}
namespace Ch11Ex02
{
class Program
{
static void Main(string[] args)
{
Animals animalCollection = new Animals();
animalCollection.Add(new Cow("花花"));
animalCollection.Add(new Chicken("华华"));
foreach (Animal item in animalCollection)
{
item.Feed();
}
}
}
}
- 以上实现了Animals类中强类型化的Animal对象集合。
3 键控集合和IDictionary
- 除了实现IList接口外,集合还可以实现类似的IDictionary接口,允许项通过键值(如字符串名)进行索引,而不是通过一个索引。这也可以使用索引符来完成,但这次使用的索引符参数是一个与存储的项相关联的键,而不是int索引,这样集合就更便于用户使用了。
class Animals2 : DictionaryBase
{
public void Add(string newID, Animal newAnimal) => Dictionary.Add(newID, newAnimal);
public void Remove(Animal newAnimal) => Dictionary.Remove(newAnimal);
public Animals2() { }
public Animal this[string animalID]
{
get { return (Animal)Dictionary[animalID]; }
set { Dictionary[animalID] = value; }
}
}
using System;
using System.Collections;
namespace Ch11Ex02
{
class Program
{
static void Main(string[] args)
{
Animals animalCollection = new Animals();
animalCollection.Add(new Cow("花花"));
animalCollection.Add(new Chicken("华华"));
foreach (Animal item in animalCollection)
{
item.Feed();
}
Animals2 animalDictionary = new Animals2();
animalDictionary.Add("1",new Cow("花花"));
animalDictionary.Add("3",new Chicken("华华"));
foreach (DictionaryEntry myEntry in animalDictionary)
{
Console.WriteLine(myEntry.Value.ToString());
((Animal)myEntry.Value).Feed();
}
}
}
}
4 迭代器
using System;
using System.Collections;
namespace Ch11Ex03
{
public class Primes
{
private long min;
private long max;
public Primes() : this(2, 100) { }
public Primes(long minimum,long maximum)
{
if (minimum < 2)
min = 2;
else
min = minimum;
max = maximum;
}
public IEnumerator GetEnumerator()
{
for (long possiblePrime = min; possiblePrime <= max; possiblePrime++)
{
bool isPrime = true;
for (long possibleFactor = 2; possibleFactor <= (long)Math.Floor(Math.Sqrt(possiblePrime)); possibleFactor++)
{
long remainderAfterDivision = possiblePrime % possibleFactor;
if (remainderAfterDivision==0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
yield return possiblePrime;
}
}
}
}
class Program
{
static void Main(string[] args)
{
Primes primesFrom2To1000 = new Primes(2, 1000);
foreach (long item in primesFrom2To1000)
{
Console.Write($"{ item} ");
}
Console.ReadKey();
}
}
}
public new IEnumerator GetEnumerator()
{
foreach (object animal in Dictionary.Values)
{
yield return (Animal)animal;
}
}
foreach (Animal a in animalDictionary)
{
a.Feed();
}