Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 81 - When to use a dictionary over list in c#

Suggested Videos 
Part 78 - Sort a list of complex types in c#
Part 79 - Sort a list of complex types using Comparison delegate
Part 80 - Some useful methods of List collection class



Find() method of the List class loops thru each object in the list until a match is found. So, if you want to lookup a value using a key, dictionary is better for performance over list. So, use dictionary when you know the collection will be primarily used for lookups.



Example code used in the demo
public class Program
{
    public static void Main()
    {
        Country country1 = new Country()
        {
            Code = "AUS",
            Name = "AUSTRALIA",
            Capital = "Canberra"
        };

        Country country2 = new Country()
        {
            Code = "IND",
            Name = "INDIA ",
            Capital = "New Delhi"
        };

        Country country3 = new Country()
        {
            Code = "USA",
            Name = "UNITED STATES",
            Capital = "Washington D.C."
        };

        Country country4 = new Country()
        {
            Code = "GBR",
            Name = "UNITED KINGDOM",
            Capital = "London"
        };

        Country country5 = new Country()
        {
            Code = "CAN",
            Name = "CANADA",
            Capital = "Ottawa"
        };

        //List<Country> listCountries = new List<Country>();
        //listCountries.Add(country1);
        //listCountries.Add(country2);
        //listCountries.Add(country3);
        //listCountries.Add(country4);
        //listCountries.Add(country5);

        Dictionary<stringCountry> dictionaryCountries = new Dictionary<string, Country>();
        dictionaryCountries.Add(country1.Code, country1);
        dictionaryCountries.Add(country2.Code, country2);
        dictionaryCountries.Add(country3.Code, country3);
        dictionaryCountries.Add(country4.Code, country4);
        dictionaryCountries.Add(country5.Code, country5);

        string strUserChoice = string.Empty;
        do
        {
            Console.WriteLine("Please enter country code");
            string strCountryCode = Console.ReadLine().ToUpper();

            // Find() method of the list class loops thru each object in the list until a match
            // is found. 
So, if you want to lookup a value using a key dictionary is better 
            // for performance over list. 
            // Country resultCountry = listCountries.
            //                                    Find(country => country.Code == strCountryCode);

            Country resultCountry = dictionaryCountries.ContainsKey(strCountryCode) ?                 dictionaryCountries[strCountryCode] : null;

            if (resultCountry == null)
            {
                Console.WriteLine("The country code you enetered does not exist");
            }
            else
            {
                Console.WriteLine("Name = " + resultCountry.Name + " Captial =" + resultCountry.Capital);
            }

            do
            {
                Console.WriteLine("Do you want to continue - YES or NO?");
                strUserChoice = Console.ReadLine().ToUpper();
            } 
            while (strUserChoice != "NO" && strUserChoice != "YES");
        }
        while (strUserChoice == "YES");
    }
}

public class Country
{
    public string Name { get; set; }
    public string Code { get; set; }
    public string Capital { get; set; }
}

No comments:

Post a Comment

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.