-1

I have a heap of byte/string values.

I want a function/collection to look up one based on the other. ie: provide a byte and get the corresponding string back. Provide a string, get the corresponding byte back.

How can I do this?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Yabbie
  • 491
  • 1
  • 4
  • 16

1 Answers1

1

You could create a simple class to hold the value pair (or even a Tuple would work fine):

public class Item
{
    public string StrVal { get; set; }
    public byte ByteVal { get; set; }
}

Then you could store them in a List<Item> and access them like:

var items = new List<Item>
{
    new Item {StrVal = "first", ByteVal = 1},
    new Item {StrVal = "second", ByteVal = 2},
    new Item {StrVal = "third", ByteVal = 3},
};

// Provide a byte, get the corresponding string back:
byte byteToFind = 2;
string resultStr = items.Where(i => i.ByteVal == byteToFind )
    .Select(i => i.StrVal).FirstOrDefault();

// Provide a string, get the corresponding byte back:
byte resultByte = items.Where(i => i.StrVal == resultStr)
    .Select(i => i.ByteVal).FirstOrDefault();
Rufus L
  • 36,127
  • 5
  • 30
  • 43