Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Why would anyone ever use a dictionary with two integers?
It seems to me like this would just create an indexed collection of integers, where the "key" is the integer index and the "value" is the integer.
Or, in other words, it would be the same as a list of integers.
Now, I suppose having a dictionary allows you to set custom integer keys, so for example you could have pairs like (1,2) (7,3), etc. but this still doesn't make much sense. But when would it actually be practical/useful to use a dictionary as opposed to List?
For example, if you have a database-table with customers.
And you have another table with orders.
You want to get the number of different orders of every customer from the database.
Then you could save it as a Dictionary<int, int>, where the first int is the customer's id and the second int is the number of different orders they have.
The TKey (on a Dictionary<TKey, TValue>) is not the index. It is a key value, you do not need to follow a specifique order in the TKey. The TKey is just a value you use to get an excatly item on the dictionary. To access an item of the dictionary by index, you can get the index by keys collections. For sample:
int key = dictionary.Keys.ElementAt(2); // the 3'rd element
var value = dictionary[key]; // the the value by the key
A List<T> is a list of value and you can access by the index.
var value = list[2]; // get the 3'rd element
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I have a set of data that I should be able to filter by two keys, which are strings.
So I've been wondering what is the faster way to do it.
The most obvious way is a nested dictionary
Dictionary<string, Dictionary<string, Data>>
but I've been also thinking about either using a tuple
Dictionary<(string, string), Data>
or combining the strings to create a key
string.Format("{0}:{1}", key1, key2)
Dictionary<string, Data>
Now this option I doubt is faster, since every lookup is gonna be coupled with the formatting of the key
There's also the option of creating a struct, which will act as the key
dataKey(key1, key2)
Dictionary<dataKey, data>
Or maybe for such case something else than a dictionary would be a better fit?
In that case I'd add that in the actual application, I first search for key1+key2 combination, and if there's no matching data, I find it using only the key2 and use a placeholder key1.
Nested dictionary lookup is a 2 lookup operations.
If you combine your two keys into one, you will avoid the second lookup operation, at the cost of a new string allocation.
It should be more efficient to have a specific structure as key that contain the two keys, and to implement GetHashCode() and Equals()
But as said by Jeroen, you should write you own benchmarks with tools like Benchmarkdotnet for real figures.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am making a game in C# and I want to be able to create items on the fly and save them to a dictionary.
My question is how do I create a new object form a base class and save it without having like 3 swords in the dictionary all with the name sword?
Essentially the code will look something like:
Sword sword = new Sword();
How do I then save the above new item to a dictionary with a unique object name?(or at least different) Or can I use an ID based system and just keep track of how many swords I have created and set the ID to something like "sword" + swordcounter
Your thought about using a static int to uniquely identify swords could work. A better way might be using GUIDs as an identifier property for all the classes in your game, as GUIDs are effectively guaranteed to be unique. The advantage there is you can use the GUIDs as keys in a dictionary (key type:GUID value type: Object), and store different types of objects in that dictionary (rather than just Swords).
There are a lot of possible approaches, i suggest you to do an Item class as a base for every item and then create a Sword : Item class and store inside a list or an array or a dictionary, every item you make with an ID.
You can still use the array position to track your items but i prefer the ID approach ^^
Two angles to my answer:
Angle #1 - Why Dictionary? What does using a Dictionary give you that something like List doesn't? I mean, unless you plan on having millions of different items and really need that performance boost on being able to binary find on a specific ID-Value, I'd think List (or Array) would suit you just fine.
Angle #2 - GUID. If you're generating a meaningless ID value for each item, you could just use a GUID:
System.Guid.NewGuid();
That would give you a unique value for any given item which you could use as the Key for your dictionary item.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
how do i get value(i) - value(i-1) (or compare adjacent ) in a data dictionary? I need to flag the adjecent values which are same in the same dictionary
Dictionaries are unordered. There is no position for the items in it, and as a result, there is no such thing as an "adjacent" item.
Basically, you shouldn't be doing this with a regular dictionary. Adjacency/indexing is not a concept for normal dictionaries. If you really need something, use an OrderedDictionary which is some sort of hybrid array/dictionary.
int? lastValue = null;
int diff;
foreach( KeyValuePair<int, int> kvp in myDictionary )
{
if(lastVaue != null)
diff = kvp.value - lastValue;
lastValue = kvp.value;
}
As stated by other foreach (GetEnumerator()) is loose in a Dictionary
If you want order you should use another collection.
OrderedDictionary
If the keys are ordered I think you could use
myDictionary.OrderBy(x => x.Key)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have a list of randomly generated numbers (let's call it numbers). I have another list of int of the same number of elements (I'll call it positions). The purpose of positions is to store the ordinal positions of the element. How do I go about this?
This is what I have thought about it:
Creating a copy of numbers called copy
Having to link it to numbers (sort of)
Sorting copy
Getting the indices of copy
Storing the associated indices in positions
The second step is what I don't know how to go about.
I'm not sure if what I have thought is the best. So I'll actually prefer a better implementation of this.
Follow these steps:
Create Pairs of int, for Example Tuple<int,int>
For each element in numbers create a pair with the number and an increasing index
Sort your pairs with the first number as a key
Now you'll find the positions in the second int of your sorted pairs
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
What is the suggested key, value container where the key is always of type string, and the value is a type? The container will not be larger than 100 items. Thanks!
You should definitely go with standard Dictionary<TKey, TValue>. The only improvement you can make is based on your there will be no more than 100 items condition.
Use new Dictionary<string, MyType>(int capacity) constructor when creating dictionary, it will prevent underlying storage from reallocating when new items are added.
var dict = new Dictionary<string, MyType>(100);
I have trouble suggesting anything other than the standard .net dictionary if you need concurrency there is also a ConcurrentDictionary