Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate tuples and sets in C#?

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?

like image 912
Hesam Qodsi Avatar asked Apr 11 '10 06:04

Hesam Qodsi


People also ask

How do you make a set of tuples?

To create a tuple in Python, use the parentheses ( ), separated by commas. The parentheses are optional. A tuple can have many elements with different data types like integer, float, list, string, etc. To create a set in Python, use the parentheses { }, separated by commas.

Can you have a set of tuples?

Tuple — mutable elements Although tuples are immutable, they can contain mutable elements such as lists or sets.

Are there tuples in C?

Tuples has a sequence of elements of different data types. It was introduced to return an instance of the Tuple<T> with no need to specify the type of each element separately.

How do you make a two element tuple?

You can create 2-tuple using Tuple<T1, T2>(T1, T2) constructor. It initializes a new instance of the Tuple<T1,T2> class. But when you create a tuple using this constructor then you have to specify the type of the element stored in the tuple.


3 Answers

.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 similar to a Tuple< T1, T2 >, with the main difference being that KeyValuePair requires that TKey is unique.

like image 138
Kevin Crowell Avatar answered Sep 23 '22 05:09

Kevin Crowell


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"
  };
like image 44
mjv Avatar answered Sep 26 '22 05:09

mjv


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)

like image 43
AakashM Avatar answered Sep 23 '22 05:09

AakashM