C# Storing string,int,string in an accessable variable

前端 未结 5 1823
故里飘歌
故里飘歌 2021-01-26 05:16

I need to save a class with list of countries in statics for caching.

the data is built with

string shortName //Primary Key - (IL or UK for example)
int         


        
相关标签:
5条回答
  • 2021-01-26 05:45

    You should create a custom type:

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

    and then store the countries in a Dictionary<string, Country> from which you'll be able to do:

    var UK = _countries["UK"];
    UK.ID...
    UK.LongName...
    
    0 讨论(0)
  • 2021-01-26 05:47

    why wont you make your own class?

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

    then just make another class, that will contain methods you need

    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();
    }
    
    0 讨论(0)
  • 2021-01-26 05:50

    I suggest the use of static methods:

    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();
       }
    }
    
    0 讨论(0)
  • 2021-01-26 05:56

    How about using a struct or class?

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

    You can then store your countries in a generic List, for example:

    List<Country> countries = new List<Country>();
    countries.Add(new Country()
    {
        shortName = "UK",
        ID = 1,
        longName = "United Kingdom",
    });
    

    Implementing your given methods then becomes very straightforward:

    Country getByShortName(string shortName)
    {
        foreach (Country country in countries)
        {
            if (country.shortName == shortName)
            {
                return country;
            }
        }
        return null;
    }
    
    0 讨论(0)
  • 2021-01-26 05:57

    I think you need to create your own class incapsulating all three fields and to use own Collection of It. For example:

    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
    }
    

    If you like quick search then to use a pair of dictionaries:

        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
        }
    
    0 讨论(0)
提交回复
热议问题