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

前端 未结 5 1828
故里飘歌
故里飘歌 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: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 countries = new List();
    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;
    }
    

提交回复
热议问题