Best option to store (key, key and value) in C#?

Asked

Viewed 60 times

1

I am working on a tool where the user inserts information and based on some calculations in the entered value it returns a fixed value description.
Example:
User enters the value 101.
It value will be calculated based on some rules.
This value for example fits in Group A the final result after the calculation is 3 (We can have several groups A, B, C, D, E, F, G and so on...), and for each group I have some values already pre-defined.
Group A:
0 = inappropriate definition
1 = accepts all type of request
2 = awaiting validation
3 = status ok

I was storing these Values in Dictionary. Is there a simpler solution for me to take these guys dynamically or feed the Dictionary dynamically and then consume them ?

I thought of saving in a file . txt and mount the dictionary.
txt file.
A;0;Initial Status
A;1; Failure to boot
A;2;Failure to close
A;3;Status OK
Z;10; Error in memory
Z;24;Incorrect attempt
Z;54;Corrupted
Z;55;Indefinite status

1 answer

0

The dictionary is the best option for your need when you have a lot of data, it calculates the HASH (which is a fast operation, proportional to the size of the data), and stores in a Linked list (pointer linked) the items in which there is HASH collision.

Basically there are three operations:

  • Hash calculation (fast, proportional to the data size)

  • Query in a HASH table (quick, array indexing)

  • Linear query in a linked list (the fastest part)

Stay tuned if you use floating point values as a key (as they are calculated), the dictionary will only return exact matches.

As for dictionary initialization, you can do statically in code or deserialize a file (txt, csv, xml, json, yaml, ini, binary, etc...)

There is also the possibility of using a static database, where, taking the keys, you get the message with a common SQL query. Sqlite is open source and many developers use it (e.g., Chrome/Chromium).

For few data is better to make a switch even, the query ends up being more efficient than computing Hashs, or query database, but do your tests.

  • Cool Edney, Today I’m populating Dictionary. Is there any way I can do this dynamically ? Because this Dictionary to be created it needs the Group after the key and value. Ie I am creating 1 dictionary for each group. I wanted to have only one dictionary and after calculating and identifying the group I would assemble that dictionary, based on some text file or json or something else. I don’t know how to proceed.

  • Your question is a little hard to understand but I kick (based on what you wrote) that you need a : Dictionary<int, Dictionary<int, Object>. Then you can use with: values[i][j]. Edit your question and put more details, otherwise it is difficult...

  • Also, you can use a Struct as a key (with the group and value), so you will only have a dictionary. Recommended you make Struct immutable to use as dictionary key (with private fields and pass parameters in constructor).

  • I edited the question, I thought, save in a txt all the details of the messages and the groups and then mount in the dictionary Dictionary<string, Dictionary<int, string>>

  • If it is too much data (but not too much) can use an Sqlite, does not depend on server, and is very efficient.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.