C#在可访问变量中存储string、int、string
我需要在静态中保存一个包含国家列表的类以进行缓存 数据是用C#在可访问变量中存储string、int、string,c#,design-patterns,types,C#,Design Patterns,Types,我需要在静态中保存一个包含国家列表的类以进行缓存 数据是用 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<in
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<int,string>> list = new Dictionary<string, Dictionary<int,string>>();
你认为处理这门课最好的方法是什么
谢谢我认为您需要创建包含所有三个字段的自己的类,并使用自己的类集合。例如:
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 <CountryInfo>
{
//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
}
List<Country> countries = new List<Country>();
countries.Add(new Country()
{
shortName = "UK",
ID = 1,
longName = "United Kingdom",
});
class CountryInfo
{
string shortName//主键-(例如IL或UK)
int ID//Unique-没有意义,但需要保存
字符串longName//(例如以色列或英国)
}
类别CountryCollection:集合
{
//实现您需要的方法
void getByShortName();//我不知道该返回什么,我很乐意
void getById();//我可能需要它
void getAll();//我不知道返回什么,我会
}
如果您喜欢快速搜索,请使用一对词典:
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 <int, string> Ids = new Dictionary <int, string> ();
Dictionary <string, string> shortNames = new Dictionary <string, string> ();
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
}
class CountryInfo
{
string shortName//主键-(例如IL或UK)
int ID//Unique-没有意义,但需要保存
字符串longName//(例如以色列或英国)
}
类别CountryCollection
{
字典ID=新字典();
Dictionary shortNames=新字典();
无效添加(国家信息)
{
添加(info.ID,info.longName);
添加(info.ID,info.longName);
}
//实现您需要的方法
void getByShortName();//我不知道该返回什么,我很乐意
void getById();//我可能需要它
void getAll();//我不知道返回什么,我会
}
我认为您需要创建包含所有三个字段的自己的类,并使用自己的类集合。例如:
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 <CountryInfo>
{
//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
}
List<Country> countries = new List<Country>();
countries.Add(new Country()
{
shortName = "UK",
ID = 1,
longName = "United Kingdom",
});
class CountryInfo
{
string shortName//主键-(例如IL或UK)
int ID//Unique-没有意义,但需要保存
字符串longName//(例如以色列或英国)
}
类别CountryCollection:集合
{
//实现您需要的方法
void getByShortName();//我不知道该返回什么,我很乐意
void getById();//我可能需要它
void getAll();//我不知道返回什么,我会
}
如果您喜欢快速搜索,请使用一对词典:
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 <int, string> Ids = new Dictionary <int, string> ();
Dictionary <string, string> shortNames = new Dictionary <string, string> ();
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
}
class CountryInfo
{
string shortName//主键-(例如IL或UK)
int ID//Unique-没有意义,但需要保存
字符串longName//(例如以色列或英国)
}
类别CountryCollection
{
字典ID=新字典();
Dictionary shortNames=新字典();
无效添加(国家信息)
{
添加(info.ID,info.longName);
添加(info.ID,info.longName);
}
//实现您需要的方法
void getByShortName();//我不知道该返回什么,我很乐意
void getById();//我可能需要它
void getAll();//我不知道返回什么,我会
}
使用or怎么样
然后,您可以将您的国家/地区存储在通用数据库中,例如:
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 <CountryInfo>
{
//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
}
List<Country> countries = new List<Country>();
countries.Add(new Country()
{
shortName = "UK",
ID = 1,
longName = "United Kingdom",
});
用手术室怎么样
然后,您可以将您的国家/地区存储在通用数据库中,例如:
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 <CountryInfo>
{
//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
}
List<Country> countries = new List<Country>();
countries.Add(new Country()
{
shortName = "UK",
ID = 1,
longName = "United Kingdom",
});
您应该创建一个自定义类型:
public class Country
{
public string ShortName {get; set;}
public int ID {get; set;}
public string LongName {get; set;}
}
然后将国家/地区存储在字典中
您可以从中执行以下操作:
var UK = _countries["UK"];
UK.ID...
UK.LongName...
您应该创建一个自定义类型:
public class Country
{
public string ShortName {get; set;}
public int ID {get; set;}
public string LongName {get; set;}
}
然后将国家/地区存储在字典中
您可以从中执行以下操作:
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<Country> countries = new List<Country>();
public void Add(Country c)
{
countries.Add(c);
}
public List<Country> getByShortName();
public List<Country> getById();
public List<Country> getAll();
}
类国家
{
列表国家=新列表();
公共空间添加(c国)
{
添加(c);
}
公共列表getByShortName();
公共列表getById();
公共列表getAll();
}
你为什么不自己上课呢
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<Country> countries = new List<Country>();
public void Add(Country c)
{
countries.Add(c);
}
public List<Country> getByShortName();
public List<Country> getById();
public List<Country> getAll();
}
类国家
{
列表国家=新列表();
公共空间添加(c国)
{
添加(c);
}
公共列表getByShortName();
公共列表getById();
公共列表getAll();
}
我建议使用静态方法:
public class Country
{
public string ShortName {get;set;}
public int ID {get;set;}
public string LongName { get; set; }
}
public class Countries
{
static Dictionary<string, Country> dict = new Dictionary<string, Country>();
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<Country> 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();
}
}
公共类国家
{
公共字符串短名称{get;set;}
公共int ID{get;set;}
公共字符串LongName{get;set;}
}
公营国家
{
静态字典dict=新字典();
公共静态无效添加(国家/地区)
{
如果(!dict.ContainsKey(country.ShortName))
dict.Add(country.ShortName,country);
}
公共静态国家/地区GetByShortName(字符串短名称)
{
if(dict.ContainsKey(简称))
返回dict[ShortName];
返回null;
}
公共静态国家/地区GetById(int id)
{
var结果=来自dict中的国家/地区
其中country.Value.ID==ID
选择新国家/地区
{
ID=country.Value.ID,
ShortName=country.Value.ShortName,
LongName=country.Value.LongName
};
返回结果。SingleOrDefault();
}
公共静态列表GetAll()
{
var结果=来自dict中的国家/地区
选择新国家/地区
{
ID=country.Value.ID,
ShortName=country.Value.ShortName,
LongName=country.Value.LongName
};
返回result.ToList();
}
}
我建议使用静态方法:
public class Country
{
public string ShortName {get;set;}
public int ID {get;set;}
public string LongName { get; set; }
}
public class Countries
{
static Dictionary<string, Country> dict = new Dictionary<string, Country>();
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<Country> 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();
}
}
公共类国家
{
公共字符串短名称{get;set;}
公共int ID{get;set;}
公共字符串LongName{get;set;}
}
公营国家
{
静态字典dict=新字典();
公共静态无效添加(国家/地区)
{
如果(!dict.ContainsKey(country.ShortName))
dict.Add(country.ShortName,country);
}
公共静态国家/地区GetByShortName(字符串短名称)
{
if(dict.ContainsKey(简称))
返回dict[ShortName];
返回null;
}
公共统计