Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8276859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:30:31+00:00 2026-06-08T08:30:31+00:00

I have a static class in my Class Library called Lookup, I am using

  • 0

I have a static class in my Class Library called Lookup, I am using this class to look up different values (in this case Locations).

These values can number into the hundreds. Since 95% of my customers install my app on a machine without Internet access I have to assume that my applications will not have internet access nor access to a database.

So I want to know if this is an efficient way of handling this and if I am properly disposing the object when the method is done:

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 a Lookup instead of the list. I just don’t want multiple calls to this method to cause memory issues.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:30:32+00:00Added an answer on June 8, 2026 at 8:30 am

    For a more thorough assessment, you might want to consider codereview.SE.


    Some general notes on List<T> vs Dictionary<TKey, TValue> vs Lookup<TKey, TElement>

    As other answers have shown, using a List is terrible in your scenario, mainly because looking up elements will have bad performance.

    Choosing between Dictionary and Lookup isn’t hard (from MSDN, emphasis mine):

    A Lookup<TKey, TElement> resembles a Dictionary<TKey, TValue>. The difference is
    that a Dictionary<TKey, TValue> maps keys to single values, whereas a
    Lookup<TKey, TElement> maps keys to collections of values.

    You can create an instance of a Lookup<TKey, TElement> by calling ToLookup
    on an object that implements IEnumerable<T>.

    Since you will only need to map keys to single values, a Dictionary is the right choice.


    The previously accepted answer is a step in the right direction but still gets several key things wrong (edit: these problems have since been resolved).

    Strings are immutable: s.Trim() will not change s — it will return a new string meaning you need to s = s.Trim() if you are using to s afterwards, which you are.

    A static class can’t have an instance constructor: public Lookups() should be static Lookups() (static constructors are not allowed to have access modifiers — of course).

    Don’t return an empty string / an error message as a string!

    That’s going to end up as a wonderful debugging headache. You should be using Exceptions instead of passing error strings around — and you should provide a VersionExists method to check if your dictionary contains a certain version!

    Modified, safer example

    This will throw a FormatException if the parameter is empty, null or whitespace. In the event that the version doesn’t exist, the Dictionary will throw a KeyNotFoundException — a bit more helpful for debugging than string.Empty, don’t you think?

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class (called Employee. non static) defined in a class library. I
I have a static class that looks like this: namespace Argus { static class
Let's say I have a static class with a static method. Multiple threads can
In my testing project, I have a static class called FixtureSetup which I use
Say I had a library called libfoo which contained a class, a few static
I have a Wpf application project called WpfTest which references WpfTestLib (A class library).
I have a Matlab function compiled into C library. I am using this library
My situation is essentially this: I have a class called Foo which has dependencies
I have a compiled external library that I'm using in my (Objective-C++) code. This
This is a simplified version of the original problem. I have a class called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.