C#在可访问的变量中存储字符串,int,string

时间:2011-09-27 10:32:25

标签: 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<int,string>> list = new Dictionary<string, Dictionary<int,string>>();

这是我在课堂上需要的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

您认为处理此课程的最佳方式是什么?

感谢

5 个答案:

答案 0 :(得分:2)

如何使用structclass

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中,例如:

List<Country> countries = new List<Country>();
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;
}

答案 1 :(得分:1)

我认为您需要创建自己的类来封装所有三个字段并使用自己的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 <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
}

如果你喜欢快速搜索,那么使用一对词典:

    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
    }

答案 2 :(得分:1)

您应该创建自定义类型:

public class Country
{
  public string ShortName {get; set;}
  public int ID {get; set;}
  public string LongName {get; set;}
}

然后将国家/地区存储在Dictionary<string, Country> 从中你可以做到:

var UK = _countries["UK"];
UK.ID...
UK.LongName...

答案 3 :(得分:1)

为什么不自己上课?

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();
}

答案 4 :(得分:0)

我建议使用静态方法:

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();
   }
}