思路
用枚举取代简单的选择分值,更容易理解
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 字典_枚举
{
class Program
{
enum Leixing {
类型1 = 0,
类型2 = 1,
类型3 = 2
}
static void Main(string[] args)
{
Dictionary<Leixing, A> keyValuePairs = new Dictionary<Leixing, A>();
Console.WriteLine("ddd");
A a1 = new A("类型1");
A a2 = new A("类型2");
A a3 = new A("类型3");
keyValuePairs.Add(Leixing.类型1, a1);
keyValuePairs.Add(Leixing.类型2, a2);
keyValuePairs.Add(Leixing.类型3, a3);
foreach (KeyValuePair<Leixing, A> keyValuePair in keyValuePairs) {
keyValuePair.Value.fun();
}
keyValuePairs[Leixing.类型3].fun();
Console.ReadKey();
}
class A {
string str;
public A(String str) {
this.str = str;
}
public void fun() {
Console.WriteLine(str);
}
}
}
}
运行结果