Using a list, search, or dictionary for a large list of data

I have a static class in my class library called Lookup, I use this class to search for different values ​​(in this case Locations).

These values ​​can be in the hundreds. Since 95% of my clients install my application on a machine without Internet access, I must assume that my applications will not have Internet access or database access.

So, I want to know if this is effective for this and if I dispose of the object correctly when this method is executed:

THE CODE:

using System;
using System.Collections.Generic;

namespace FunctionLibrary
{
    public static class Lookups
    {
        private static List<Vers> Versions;

        public static string GetVersion(string s)
        {
            string retValue = string.Empty;
            Versions = new List<Vers>();

            try
            {
                if (s.Trim().Length > 0)
                {

                    GetVersions();
                    retValue = Versions.Find(ver => ver.VersionNumber == s).VersionLiteral;

                    if (string.IsNullOrEmpty(retValue))
                    {
                        retValue = string.Format("{0} is an Unknown Version Number", s);
                    }
                }
                else
                {
                    retValue = "No version number supplied";
                }
            }
            catch
            {
                retValue = string.Format("{0} is an Unknown Version Number", s);
            }
            finally
            {
                Versions.Clear();
                Versions = null;
            }
            return retValue;
        }

        private static void GetVersions()
        {
            Versions.Add(new Vers() { VersionNumber = "0000", VersionLiteral = "Location 1" });
            Versions.Add(new Vers() { VersionNumber = "0001", VersionLiteral = "Location 2" });
            Versions.Add(new Vers() { VersionNumber = "0002", VersionLiteral = "Location 3"});
            Versions.Add(new Vers() { VersionNumber = "0003", VersionLiteral = "Location 4"});
            Versions.Add(new Vers() { VersionNumber = "0004", VersionLiteral = "Location 5"});
            Versions.Add(new Vers() { VersionNumber = "0005", VersionLiteral = "Location 6"});
            Versions.Add(new Vers() { VersionNumber = "0006", VersionLiteral = "Location 7"});
            Versions.Add(new Vers() { VersionNumber = "0007", VersionLiteral = "Location 8"});
        }
    }

    public class Vers
    {

        public string VersionLiteral { get; set; }
        public string VersionNumber { get; set; }
   }
}

I am also wondering if I should use a dictionary or search instead of a list. I just don't want multiple calls to this method to cause memory problems.

+5
source share
3

codereview.SE.


List<T> vs Dictionary<TKey, TValue> vs Lookup<TKey, TElement>

, , , .

Dictionary Lookup ( MSDN, ):

A Lookup<TKey, TElement> a Dictionary<TKey, TValue>. - a Dictionary<TKey, TValue> , Lookup<TKey, TElement> .

Lookup<TKey, TElement>, ToLookup , IEnumerable<T>.

, Dictionary.


, (: ).

: s.Trim() s - string, , s = s.Trim(), s, .

: public Lookups() static Lookups() ( ), .

/ !

. , - VersionExists, , !

,

FormatException, , . , , Dictionary KeyNotFoundException - , string.Empty, ?

public static class Lookups
{
    private static Dictionary<string, Vers> Versions;

    static Lookups()
    {
        Versions = new Dictionary<string, Vers>
        {
            {"0000", new Vers {VersionNumber = "0000", VersionLiteral = "Location 1"}},
            {"0001", new Vers {VersionNumber = "0001", VersionLiteral = "Location 2"}},
            {"0002", new Vers {VersionNumber = "0002", VersionLiteral = "Location 3"}},
            {"0003", new Vers {VersionNumber = "0003", VersionLiteral = "Location 4"}},
            {"0004", new Vers {VersionNumber = "0004", VersionLiteral = "Location 5"}},
            {"0005", new Vers {VersionNumber = "0005", VersionLiteral = "Location 6"}},
            {"0006", new Vers {VersionNumber = "0006", VersionLiteral = "Location 7"}},
            {"0007", new Vers {VersionNumber = "0007", VersionLiteral = "Location 8"}}
        };
    }

    public static bool VersionExists(string versionNumber)
    {
        return Versions.ContainsKey(versionNumber);
    }

    public static string GetVersion(string s)
    {
        if (string.IsNullOrWhiteSpace(s)) 
            throw new FormatException("Empty version number!");
        return Versions[s.Trim()].VersionLiteral;
    }
}
+14

:

using System;
using System.Collections.Generic;

namespace FunctionLibrary
{
public static class Lookups
{
    private static Dictionary<string, Vers> Versions = new Dictionary<string, Vers>();

    static Lookups() //Static constructor
    {
        CreateVesions();
    }

    public static string GetVersion(string s)
    {
        string retValue = string.Empty;

                    s = s.Trim();

        if (s.Length > 0 && Versions.ContainsKey(s))
        {
            retValue = Versions[s].VersionLiteral;
        }

        if (string.IsNullOrEmpty(retValue))
        {
            retValue = string.Format("{0} is an Unknown Version Number", s);
        }

        return retValue;
    }

    private static void CreateVesions()
    {
        Versions["0000"] = new Vers() { VersionNumber = "0000", VersionLiteral = "Location 1" };
        Versions["0001"] = new Vers() { VersionNumber = "0001", VersionLiteral = "Location 2" };
        Versions["0002"] = new Vers() { VersionNumber = "0002", VersionLiteral = "Location 3" };
        Versions["0003"] = new Vers() { VersionNumber = "0003", VersionLiteral = "Location 4" };
        Versions["0004"] = new Vers() { VersionNumber = "0004", VersionLiteral = "Location 5" };
        Versions["0005"] = new Vers() { VersionNumber = "0005", VersionLiteral = "Location 6" };
        Versions["0006"] = new Vers() { VersionNumber = "0006", VersionLiteral = "Location 7" };
        Versions["0007"] = new Vers() { VersionNumber = "0007", VersionLiteral = "Location 8" };
    }
}

public class Vers
{

    public string VersionLiteral { get; set; }
    public string VersionNumber { get; set; }
}

}

:

  • ,
  • GetVersion
+3

List . .

LOT.

, Lookup , .

, VersionNumber , Lookup.

You can simply convert your list to a dictionary or search once:

var versionsDictionary = versionsList
                             .ToDictionary(x => x.VersionNumber);

So, you do this once (possibly when the application loads), and then use it everywhere:

var myVersion = versionsDictionary[givenNumber];
Console.WriteLine(myVersion.VersionLiteral);

Again, if you need Lookup, replace ToDictionary with ToLookup;)

+2
source

All Articles