Is there any data structure in C# that is like a dictionary but that only has a key and doesn't have a value. I basically want a list of integers that I can quickly lookup and see if a certain value is in the list. Granted, for my current use, a List would not cause any performance problem, but it just doesn't seem to fit well with the intent of what my code is doing.
Yes, it's called a HashSet<T>, and available in version 3.5 of the .NET framework. If you use .NET version 2.0, you can use a Dictionary and set values to null.
If 3.5 is not an option you could do something like Dictionary < int, int > and simply ignore the value. i've done this in 2.0 and i tend to set the value to the same as the key.
If you're not targeting .NET 3.5, Power Collections (open source) also provides a Set implementation.
or use a SortedList where values have to be unique
Related
I have a the following requirement to develop and am wondering what the fastest collection I could use in .net would be.
"To search a static collection of strings to find all string that start with a given string, the string can be in order".
Would a hashtable be the best to use? This is being done in c# .net 4.0.
Thanks
It is premature to optimize collection types - you don't even have working code!
Use a convenient collection type (Dictionary<TKey,TValue>, List<Tuple<T1,T2>> or whatever) - once you have working code, if there is a performance problem:
Profile the code
Identify the hotspots
If the collection you have selected is a hotspot, consider changing it.
Profile after the change to see you have fixed the problem
To find strings with a given prefix quickly, try a trie.
The title pretty much sums it up. Now that we have List<T>'s why would anyone want to use an ArrayList instead? The only reason I can think of is if you are forced to use an old version of .NET before List<T> was implemented?
As you said, if for some reason you are stuck with .Net 1.1 then you don't have a choice.
Your question seems to indicate that there is a choice. So then there is no reason to userArrayList. Anything that ArrayList can do, List<T> can do as well, if not better.
Short answer, no. ArrayList was from when .NET didn't support generics. List is the more flexible (generic) way to handle lists. In fact, I don't think Silverlight even supports ArrayLists at all.
ArrayList is mostly for backward compatibility. In a project when there is a deadline, you may have the time to convert everything from ArrayList to generic List.
Another reason is that you may be using a library that is written in .NET 1.1. So you may force to use ArrayList in your code, and most likely convert it to a generic List for easy coding.
However, there are some differences and you may want to read this:
.NET: ArrayList vs List
You said it yourself: backwards compatibility. There's no use for it any more, but it clearly can't be removed from the BCL as it'd break existing code that for whatever reason must be compiled against .NET 1.1.
The only time I've ever used an ArrayList since .Net 2.0 was for use with the built-in My.Settings feature in vb.net, and I've since learned I probably could have shoehorned a generic list in there.
I have customized collection for indexing reason, its an implement ion of Idicationary (non generic). This is used to hold string based key and object based value.
Now please pardon my ignorance, I have just came out of the cave.
I want use an adapter between, this is a linq adapter which should take linq queries and perform operation on this existing IndexedDictionary.
Why SO ?
This was designed for a .net 2.0 application, now slowly and steadily we are moving toward 4.0 as a part of natural evolution, so we are taking side-by-side aproach so every thing written previously should exist and 4.0 features should be implemented as adapter where ever possible.
I will summarize what I want to cut long story short, I have an existing .net 2.0 IDictionary implementation. Now I would like to use it with LINQ so that can I can take full advantage of expressions. How can I do this ?
It seems to me that you don't need an adapter. An adapter is for transforming expression trees (IQueryable) to communicate with an underlying data source. For in-memory collections, the existing extension methods such as LINQ to Objects, LINQ to XML and LINQ to DataSets will normally do. If this is not enough, you can write your own extension methods or write instance LINQ methods on your type.
Not sure that I fully understand what your purpose is and what the code needs to do. LINQ is very powerful, you probably don't need an adapter.
For going from LINQ results to Dictionary there is a ToDictionary method in LINQ. See http://www.hookedonlinq.com/ToDictionaryOperator.ashx
Cheers
Chris Farrell
You don't need to do anything to the .Net 2.0 Dictionary, just use it like any other LINQ. It implements the IEnumerable<T> interface so it can be used by LINQ. Note that when you iterate through the dictionary using LINQ, the elements will not be in "order" and you will have to use a KeyValuePair<TKey, TValue>.
I'm working on improving my skills in other languages, coming from using c++ as my primary programming language. My current project is hammering down C#.net, as I have heard it is a good in-between language for one who knows both c++ and VB.net.
Typically when working with an unknown number of elements in c++ I would declare my variable as a vector and just go from there. Vectors don't seem to exist in c#, and in my current program I have been using arraylists instead, but I'm starting to wonder if it's a good habit to use arraylists as I read somewhere that it was a carryover from .net 1.0
Long question short- what is the most commonly used listing type for c#?
If you target pre .NET 2.0 versions, use ArrayList
If you target .NET 2.0+ then use generic type List<T>
You may need to find replacements for other C++ standard containers, so here is possible mapping of C++ to .NET 2.0+ similar types or equivalents:
std::vector - List<T>
std::list - LinkedList<T>
std::map - Dictionary<K, V>
std::set - HashSet<T>
std::multimap - Dictionary<K, List<V>>
I would recommend you explore the System.Collections namespace, especially the System.Collections.Generics set of objects. The built-in functionality can be strongly typed across the various Lists, Dictionaries and NameValueCollections to provide you with a wide range of capabilities. They are also extendable so if they don't do EXACTLY what you need, you just extend them and add the new functionality.
That'd be List<T>, I suppose. ArrayList is the non-generic type and—as you correctly observed—a leftover from the .NET 1 times. Starting with .NET 2 you can use Generics and therefore List<T>.
Short answer: List<T>. You can find the docs here.
I'm working on a something related to roughset right now. The project uses alot of sets operation and manipulation. I've been using string operations as a stop gap measure for set operation. It has worked fine until we need to process some ungodly amount of data ( 500,000 records with about 40+ columns each ) through the algorithm.
I know that there is no set data structure in .net 2.0(2.0 was the latest when I started the project) I want to know if there is any library that offer fast set operation in .net c# or if 3.5 has added native set data structure.
Thanks .
.NET 3.5 already has a native set data type: HashSet. You might also want to look at HashSet and LINQ set operators for the operations.
In .NET 1.0, there was a third party Set data type: Iesi.Collections which was extended with .NET 2.0 generics with Iesi.Collections.Generic.
You might want to try and look at all of them to see which one would benefit you the most. :)
LINQ supports some set operations. See LINQ 101 page for examples.
Also there is a class HashSet (.NET 3.5)
Here is Microsoft guidelines for set operations in .NET:
HashSet and LINQ Set Operations
List of set operations supported by HasSet class:
HashSet Collection Type
Update: This is for .Net 2.0. For .Net 3.5, refer posts by aku, Jon..
This is a good reference for efficiently representing sets in .Net.
It may be worth taking a look at C5, it's a generic collection library for .NET which includes sets.
Note that I haven't looked into it much, but it seems to be a pretty fantastic collection library.
Try HashSet in .NET 3.5.
This page from a member of the .NET BCL team has some good information on the intent of HashSet
I have been abusing the Dictionary class in .NET 2.0 as a set:
private object dummy = "ok";
public void Add(object el) {
dict[el] = dummy;
}
public bool Contains(object el) {
return dict.ContainsKey(el);
}
You can use Linq to Objects in C# 3.0.
You ever think about sing F#? This seems like a job for a functional programming language.
You should take a look at C5 Generic Collection Library. This library is a systematic approach to fix holes in .NET class library by providing missing structures, as well as replacing existing ones with set of well designed interfaces and generic classes.
Among others, there is HashSet<T> - generic Set class based on linear hashing.