none
Find item value of class in dictionary? RRS feed

  • Question

  • Dear All,

    I have defined a class which associates endonym and CultureCountryCode.

    And these data are put in dictionary 

    I would like to use the CultureCountryCode property to find the corresponding endonym,

    How to implement in foreach() condition? Thanks.


    Thanks and Best regards,

    E-John

    namespace TestClassInDictionary
    {
        class Program
        {
            class LanguageCode
            {
                // native name
                public string Endonym { get; private set; }
                // The CultureInfo class specifies a unique name for each culture, based on RFC 4646.
                // The name is a combination of
                // an ISO 639 two-letter lowercase culture code associated with a language
                // and
                // an ISO 3166 two-letter uppercase subculture code associated with a country or region.
                public string CultureCountryCode { get; private set; }
                public LanguageCode(string endonym, string cultureCountryCode)
                {
                    Endonym = endonym;
                    CultureCountryCode = cultureCountryCode;
                }
            }
    
            static void Main(string[] args)
            {
                Dictionary<int, LanguageCode> supportUILanguages = new Dictionary<int, LanguageCode>()
                {
                    {1, new LanguageCode("English", "en-US") },
                    {2, new LanguageCode("中文(繁)", "zh-TW") },
                    {3, new LanguageCode("русский", "ru-RU") }
                };
    
                // Use CultureCountryCode to findout Endonym? 
                // How to do?
                // ??? foreach (LanguageCode l in supportUILanguages)
                {
                    Console.WriteLine("Native name" + l.Endonym);
                }
            }
        }
    }
    

    Friday, May 4, 2018 6:25 AM

Answers

  • Dear All,

    I got it, it should be as follows,

    Thanks and Best regards,

    E-John

    ...
                foreach (KeyValuePair<int, LanguageCode> l in supportUILanguages)
                {
                    Console.WriteLine("Native name" + l.Value.Endonym);
                }
    ...

    • Proposed as answer by Petr B Friday, May 4, 2018 7:06 AM
    • Marked as answer by E-John Friday, May 4, 2018 7:09 AM
    Friday, May 4, 2018 6:41 AM