This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Which collection for storing unique strings?
I am currently using a Dictionary<string, bool> to store a list of unique identifiers. These identifiers do not need to have any data associated with them - I am just using the Dictionary to be able to quickly check for duplicates.
Since I only need keys, and no values, is a Dictionary the way to go here, or is there another collection I don't know about that would be better suited?
.NET 3.5 includes the HashSet<T> collection type, which sounds like what you want.
HashSet<T>
Related
This question already has answers here:
The order of elements in Dictionary
(6 answers)
Closed 8 years ago.
When iterating over a dictionary in C#, are we guaranteed that the order will be same every time we iterate this dictionary?
foreach (var key in myDict.Keys)
{
...
}
According to documentation, no, there is no guarantee:
The order of the keys in the Dictionary<TKey, TValue>.KeyCollection is unspecified,but it is the same order as the associated values in the Dictionary<TKey, TValue>.ValueCollection returned by the Values property.
This question already has answers here:
YouTube URL algorithm?
(11 answers)
Closed 3 years ago.
I noticed on YouTube their keys look like this "BwgT06NY1FE". I was just wondering how is this type of key created? Is this based on a GUID?
This is most likely Base36. All letters and numbers. It's pretty common, because you can use a "SERIAL" in a database and just increment it, and then just parse from Base36 in your URL.
It makes for nice URLs (bit.ly also uses this format), but has some drawbacks. Ie, you wouldn't want to use it for any sort of private data because people can just type in a random number and get a result (it's unlikely someone could guess a GUID in use by your database unless they try a few billion)..
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What problem was the tuple designed to solve?
Could someone please show/describe an appropriate use of the System.Tuple class?
I have looked on MSDN and the description there doesn't show any practical uses.
From the MSDN on the 2-tuple:
A tuple is a data structure that has a specific number and sequence of values. The Tuple class represents a 2-tuple, or pair, which is a tuple that has two components. A 2-tuple is similar to a KeyValuePair structure.
In the Tuple<T1, T2> case, It is a generalization of the KeyValuePair<,> data structure.
Tuples are basically generic data structures. You can use them to avoid defining your own structure. An example would be for returning multiple values from a function.
It is a very useful tool in function-oriented/functional programming.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Immutable List in C#
Is it possible to make a list immutable
You can use ReadOnlyCollection<T> instead.
List<T>.AsReadOnly() returns a readonly wrapper, so that you can't add/remove/replace elements.
To be truly immutable, the type T must be an immutable Type.
This question already has answers here:
SortedList<>, SortedDictionary<> and Dictionary<>
(6 answers)
Closed 9 years ago.
I have a large collection of small objects, each has a unique string ident. I need to decide which class to use.
MSDN says about the first two
The two classes have similar object
models, and both have O(log n)
retrieval. Where the two classes
differ is in memory use and speed of
insertion and removal
Since I rarely insert, mostly just retrieve it seems both are good for me. What about the plain old Dictionary?
Plain-old dictionary is the best option if you're not interested in sorting (since it's O(1) retrieval). If you're not going to modify the list much you should use SortedList since it uses less memory.