I want to use some features of python like as Tuples and Sets in c#. should I implement them? or there are already implemented? could anybody knows a library of dynamic data structures for .net languages?
.NET 3.5 has HashSet.
.NET 4.0 will have a Tuple class. As noted in the article, earlier version of .NET do contain KeyValuePair< TKey, TValue > which is just like a Tuple< T1, T2 >.
For Sets, HashSets (a .NET 3.5 feature) do the trick quite well.
A partial answer, for tuples:
- .NET 4.0 provides [some] support for tuples.
- Earlier versions of C# can use the anonymous type (I think introduced in .Net 2.0, 3.0 for sure, with all the LINQ stuff).
Neither of these approaches are as convenient as with Python; the main handicap comes from the fact that C# is statically typed. However the C# 4.0 Tuple class has factory-like static methods which make the creation of tuples easier (up to 8-tuple, i.e. tuples with 8 members). For example one can have
var customer1 = Tuple.Create("John", "Smith", 14, 5.33, "202-123-444");
Using anonymous type can be done as follow. The main drawback of this approach is that one needs to explicitly name the elements of the "tuple" (although this naming can be implicitly "projected" if the values used for initialization are "projected" from another object.
customer1 = new Customer { Name = "John", Surname = "Smith", NumberOfVisits = 14, CurrentBalance = 5.33, PhoneNr = "202-123-444" };
If you're working with a .NET Framework earlier than already mentioned, Wintellect Power Collections might prove of some interest - it has Pair
and Triple
for 2- and 3-tuples, and collections such as Set
, Bag
, and Ordered
flavours of both.
Of course, there's nothing stopping you from implementing 4.0's Tuple
yourself.
(By the way, there's nothing particularly 'dynamic' about data structures like these in and of themselves)