Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# dictionary or just keep using lists?

Tags:

c#

I'm using a list to store a pair of hexadecimal values (eg. in the list AD38F2D8, displayed as:Value_A: AD 38 F2 D8).

My question is should I use a Dictionary<string, string> or should I use Dictionary<string, NewCustomObject> to store the hexadecimal string as a pair of strings. (Value: [AD, 38, F2, D8]) instead of (Value: AD38F2D8).

It probably won't make to much of a difference between the two.

With Dictionary<string, string> I would just store each hex string in the dictionary, and then split them up in their respective pairs when I need them. if I use the Dictionary<string, NewCustomObject> I would end up first splitting the hex strings in their respective pairs and then store them in the dictionary.

My question is which should I use? Or should I just keep using lists?

It's not entirely necessary for me to use Dictionary<string, string> as I would still know which index what string is on, just that it would look nicer.

List example:

Index = 0,  Value = 3D95FF08
Index = 1,  Value = 8D932A08

Dictionary<string, string> example:

Index = 0, Key = First,  Value = 3D95FF08
Index = 1, Key = Second, Value = 8D932A08

Dictionary<string, NewCustomObject> example:

Index = 0, Key = First,  Value = 3D, 95, FF, 08
Index = 1, Key = Second, Value = 8D, 93, 2A, 08

NOTE: the Index value in each dictionary example is just to show the corespondence to the others, I know a dictionary does not have Index values but uses keys instead. it's just makes it easier to look at this example.

I came across the last Dictionary string here: C# Dictionary with two Values per Key? I was searching for it as I have used that way several times while writing python code. by storing a list of strings within a dictionary.

Thanks in advance for any help!

EDIT

Each string in either the list or dictionary, would be split in their respective bytes. EG.

BYTE   0   1   2   3 to  6 .....
HEX    AD  12  01  0000859D  .....

Byte 0 would hold the Index Byte 1 would hold the Reference Byte 2 the Flags Byte 3 to 6: The Memb_Number_ID

Therefore, if I split them up before possibly putting these in the dictionary I don't have to do it before I use them in their respective place as I would have to split them up to calculate the index, reference and flags.

The database was given to me in this format, I am forced to work with it the way it currently is, so i can not change it. and can only adapt my code to it.

above example would be outputted as:

Index  Reference Link    Flags          Memb_Number_ID
173     18               +A             John (ID: 34205)
like image 440
Raskaroth Avatar asked Oct 11 '22 00:10

Raskaroth


1 Answers

Dictionaries do not expose indexes, they don't have the concept. I'd stick with a list:

List<NewCustomObject> myHexValues;

And if you need to search through it, you can use LINQ. Only use dictionaries if you need fast access based on a defined key.

You haven't really stated how you intend to use the collection, so no one can really tell you to use one or the other as yet - I'm only guessing based on the fact you want index values.

Update: if you want easier access to elements of the hex value, you can either use a rectangular array or wrap the value in a custom type and expose properties for the different parts (your choice on struct vs class, depends on the values I suppose), and then stick that type in a list.

If you make your own type, you can expose an indexer if you wish to get the following syntax:

myCustomType[indexKey] = value;

like image 143
Adam Houldsworth Avatar answered Oct 22 '22 00:10

Adam Houldsworth