C#在可访问变量中存储string,int,string
我需要保存一个包含静态数据的国家/地区列表的类。
数据是用
string shortName //Primary Key - (IL or UK for example) int ID //Unique - has no meaning, but needs to be saved string longName //(Israel or United Kingdom for example)
我想把它保存在一个字典中:
Dictionary<string , Dictionary> list = new Dictionary<string, Dictionary>();
这是我在课堂上需要的API:
Countries.getByShortName();// I dont know what to return, I'd love some advise Countries.getById();// I might need that Countries.getAll();// I dont know what to return, I'd love some advise
您认为处理这门课程的最佳方式是什么?
谢谢
我建议使用静态方法:
public class Country { public string ShortName {get;set;} public int ID {get;set;} public string LongName { get; set; } } public class Countries { static Dictionary dict = new Dictionary(); public static void Add(Country country) { if (!dict.ContainsKey(country.ShortName)) dict.Add(country.ShortName, country); } public static Country GetByShortName(string ShortName) { if (dict.ContainsKey(ShortName)) return dict[ShortName]; return null; } public static Country GetById(int id) { var result = from country in dict where country.Value.ID==id select new Country { ID = country.Value.ID, ShortName = country.Value.ShortName, LongName = country.Value.LongName }; return result.SingleOrDefault(); } public static List GetAll() { var result = from country in dict select new Country { ID = country.Value.ID, ShortName = country.Value.ShortName, LongName = country.Value.LongName }; return result.ToList(); } }
如何使用结构或类 ?
class Country { string shortName; // Primary Key - (IL or UK for example) int ID; // Unique - has no meaning, but needs to be saved string longName; // (Israel or United Kingdom for example) }
然后,您可以将国家/地区存储在通用列表中 ,例如:
List countries = new List (); countries.Add(new Country() { shortName = "UK", ID = 1, longName = "United Kingdom", });
然后实现您给定的方法变得非常简单:
Country getByShortName(string shortName) { foreach (Country country in countries) { if (country.shortName == shortName) { return country; } } return null; }
您应该创建自定义类型:
public class Country { public string ShortName {get; set;} public int ID {get; set;} public string LongName {get; set;} }
然后将国家Dictionary
存储在您可以从中执行的Dictionary
:
var UK = _countries["UK"]; UK.ID... UK.LongName...
你为什么不自己上课?
class Country { public string shortName { get; set; } //Primary Key - (IL or UK for example) public int ID { get; set; } //Unique - has no meaning, but needs to be saved public string longName { get; set; } //(Israel or United Kingdom for example) }
然后只创建另一个类,它将包含您需要的方法
class Countries { List countries = new List (); public void Add(Country c) { countries.Add(c); } public List getByShortName(); public List getById(); public List getAll(); }
我认为你需要创建自己的类来封装所有三个字段并使用自己的Collection of It。 例如:
class CountryInfo { string shortName //Primary Key - (IL or UK for example) int ID //Unique - has no meaning, but needs to be saved string longName //(Israel or United Kingdom for example) } class CountryCollection : Collection { //Implement methods what you need void getByShortName();// I dont know what to return, I'd love void getById();// I might need that void getAll();// I dont know what to return, I'd l }
如果您喜欢快速搜索,那么使用一对词典:
class CountryInfo { string shortName //Primary Key - (IL or UK for example) int ID //Unique - has no meaning, but needs to be saved string longName //(Israel or United Kingdom for example) } class CountryCollection { Dictionary Ids = new Dictionary (); Dictionary shortNames = new Dictionary (); void Add (CountryInfo info) { Ids.Add (info.ID, info.longName); shortnames.Add(info.ID, info.longName); } //Implement methods what you need void getByShortName();// I dont know what to return, I'd love void getById();// I might need that void getAll();// I dont know what to return, I'd l }