我创建了一个名为“EngDictionary”的类。然后我在一个函数中定义一个字典 例如:
public void Dict()
{
Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("Classifieds", "Kleinanzeigen");
//d.Add("windows", 5);
}
现在我想从我的主类访问上面定义的字典以检索我的字典的键和值。请给我一些代码。我正在使用 Visual C# 2008 Express Edition,Win 应用程序
请您参考如下方法:
将字典声明为类属性。
public class Dict {
private Dictionary<string, string> dict;
public SomeDictionary { get dict; set dict = value; }
public Dict() {
dict = new Dictionary<string, string>();
dict.Add("Classifieds", "Kleinanzeigen");
}
}
在其他类中:
Dict d = new Dict();
string test = d.SomeDictionary["Classifieds"];
Console.WriteLine(test);
