samedi 21 août 2021

Selecting an element from a huge key-value pair in C# [closed]

I have a huge list of key-value pair (more than 3000). for example thery are int and string. I want to have a method or property to return the string value. There are some approachs to do this like using if-else and switch or using Dictionary<int,string>:

public string GetDescription(int value)
{
    if (value == 0) return "This is value 0";
    if (value == 1) return "This is value 1";
    if (value == 2) return "This is value 2";
    .
    .
    .
    if (value == 3000) return "This is value 3000";
}

Or:

public string GetDescription(int value)
{
    switch (parameter)
    {
        case 0: return "This is value 0";
        case 1: return "This is value 1";
        case 2: return "This is value 2";
        .
        .
        case 3000: return "This is value 3000";
    }
}

Or using Dictionary<int,string>:

public Dictionary<int, string> Descriptions
{
    get
    {
        var result = new Dictionary<int, string>();
        result.Add(0, "This is value 0");
        result.Add(1, "This is value 1");
        .
        .
        result.Add(3000, "This is value 3000");
        return result;
    }
}

Which one is better and is there a better way like saving in a table to do it?

Aucun commentaire:

Enregistrer un commentaire