Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the point of Tuple(Of T) [duplicate]

Possible Duplicate:
What's the purpose of the Tuple(T1)/Singleton in .net?

Trying to mimic a Tuple as implemented in .Net 4 (For .Net 3) I just realized there is a Tuple(Of T)? This was quite a surprize!

Why would anyone do this

Tuple<string> result = new Tuple<string>("Data");

Instead of this

return "Data";

Isn't the whole point of a tuple that its a container for "loosely related data that isnt cohesive enough to make another class"? Am I missing something?

like image 588
Maxim Gershkovich Avatar asked Sep 30 '11 06:09

Maxim Gershkovich


People also ask

What is tuple duplicate?

Tuple is a collection which is ordered and unchangeable. Allows duplicate members. Use normal brackets () for tuples. Set is a collection which is unordered and unindexed. No duplicate members.

What is the point of using a tuple?

Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable.

Is duplicate data allowed in tuple?

Tuples A Tuple represents a collection of objects that are ordered and immutable (cannot be modified). Tuples allow duplicate members and are indexed.

Does tuple remove duplicates?

Method #1 : Using set() + tuple() This is the most straight forward way to remove duplicates. In this, we convert the tuple to a set, removing duplicates and then converting it back again using tuple().


2 Answers

There are a finite number of tuple-arities in the library, so to define an 8-tuple, you use the kind with 7-elements whose 'rest' argument is a one-tuple. See

http://msdn.microsoft.com/en-us/library/dd383325.aspx

like image 52
Brian Avatar answered Oct 08 '22 21:10

Brian


This is a carry over from set theory that might not have much use for a software developer.

Tuples are simply ordered lists of elements. An N-tuple has n elements, and n can be one, which is called a singleton. You probably won't have much use for a 1-tuple in code, but I'm guessing the C# team put it in there for completeness.

http://en.wikipedia.org/wiki/Tuple#Etymology

like image 20
Adam Rackis Avatar answered Oct 08 '22 21:10

Adam Rackis