aAbrCEAEeArBABrEBAsXWC| G78465545VCWBC~B G58496V 5784VC~~BPW~ G5746466VCPZCBWPWB~B~-~BCC 5454549VCBC 56456VBX

Tuple.` From the mountain's peak to the ocean's floor, look around the world. Snow, on the mountaintop, gives way to dirt and grass. Nature forms an unbroken chain.`A single atom` attracts (and is attracted to) the next. Connections are everywhere. In a program, we combine things together (strings, ints). Tuples help us keep our logic clear and simple.`3 items.` Please note that the Tuple type is a class. Once we create the Tuple, we cannot change the values of its fields. This makes the Tuple more like a string. `Next: `In this example, we create a 3-item tuple using the special constructor syntax.`We then read the Item1, Item2 and Item3 properties. We do not modify them.`Item types.` When we create a Tuple, we specify the order and types of the fields. If you would rather have a double, byte, char Tuple, change the declaration to Tuple<double, byte, char>. `We can have value types (such as int) and reference types (such as string) inside a Tuple.`4 items.` Continuing on, a Tuple can have more complex items inside it, such as arrays. We can also pass the Tuple to other methods. `In this example, we create a four-item Tuple with two arrays—string and int arrays.`Arrays `array`Then: `We initialize those arrays inside the constructor invocation. Next we pass our Tuple variable to another method.`Var: `Why does the example use the var keyword? The reason is pure syntactic sugar. Var shortens the lines in the code example.`Var `var`6 items.` A sextuple has 6 items. To create a sextuple, use the Tuple constructor. You have to specify each type of the sextuple's items in the type parameter list. `In Visual Studio,` we can hover the mouse over the var keyword. This shows that the var "Represents a 6-tuple, or sextuple." Visual Studio further describes the tuple's individual types. `The naming of tuples is not important in many programs. But these terms can be useful when describing programs in a concise way.`Names: `Beyond septuples, we only have n-tuples. These terms will make you sound really smart.`Quote: `A tuple is an ordered list of elements. In mathematics, an n-tuple is a sequence (or ordered list) of "n" elements, where "n" is a non-negative integer.`Tuple: Wikipedia `https://en.wikipedia.org/wiki/Tuple`Tuple.Create.` Next we invoke this method. We use Create() with three arguments: a string literal, an integer and a boolean value. `Result: `The Create() method returns a class of type Tuple<string, int, bool>. It has three items.`Program: `The code does a series of tests of the Tuple. It tests Item1, Item2 and Item3.`Internals.` There is no elaborate algorithm devoted to tuple creation. The Tuple.Create method calls a constructor and returns a reference. `There is essentially no functional reason to ever call Tuple.Create. It might have more pleasing syntax.`Class implementation.` Tuple is not a struct. It is a class. It will be allocated upon the managed heap. Each class instance that is allocated adds to the burden of garbage collection. `Class `class`The properties Item1, Item2 and further do not have setters. We cannot assign them. A Tuple is immutable once created in memory.`Property: Get, Set `property`Read-only.` We must initialize all values inside a Tuple to their final values when we call the constructor. We cannot change a property (like Item1) after the constructor has run. `This limitation can lead to more maintainable code that does not rely on field changes through time. It can also reduce performance.`Performance.` I ran a benchmark on Tuple and the KeyValuePair struct. This comparison is relevant only in cases where a Tuple of two items is used. `Result: `KeyValuePair is faster when many instances are created. But Tuple is faster when the reference is passed to methods.`Tuple vs. KeyValuePair `tuple-keyvaluepair`Bytes: `When a Tuple is passed as an argument, only 4 bytes need copying. But KeyValuePair, a struct, has more bytes.`Struct `struct`ValueTuple: `The new ValueTuple syntax uses dynamic types to improve performance further.`Sort.` Tuples can be sorted. A Tuple is a great way to encapsulate units of data. But it can make sorting harder. A Comparison delegate is needed. `First: `This program creates a List and adds 3 new Tuple instances to it. We invoke the Sort method on the List.`Sort List `sort-list`We use the lambda syntax and pass in 2 arguments (a, b) and return the result of CompareTo on the Item2 string property.`To sort on the int, change the lambda to return a.Item1.CompareTo(b.Item1). A reverse sort would be b.Item2.CompareTo(a.Item2).`Return multiple values.` This is an age-old problem. A method may need to return many things, not just one. A tuple can return multiple values (with less code than a class would require). `This causes an allocation. Using ref and out parameters would be faster for a method that is hot.`Parameters `parameter`Note 2: `A Tuple has advantages. It is a reference and can be reused. Less copying is needed when passed to other methods.`ValueTuple.` This type has clear advantages over Tuple. We can specify a ValueTuple by including values in an expression (with no type names). `ValueTuple `valuetuple`A summary.` The Tuple is a typed, immutable, generic construct. That sounds impressive. Tuple is a useful container for storing conceptually-related data.`Limits.` A simple class with commented members and helper methods is more useful for important things. But Tuple shines as a short-term container.

RYBVXBBYDYQYbYJYAB{BRY%R{XBRRY{CY_ three-item tuple.BRRXTupleX<Yz, YP, bool> tuple =BRRRYqTuple<Yz, YP, bool>(X1X, XVcatVX, XtrueX);XBRRY{Access tuple properties.BRRXYmtuple.XItem1XYx1)BRR{BRRRY'tuple.Item1);BRR}BRRYmtuple.XItem2XYxXVdogVX)BRR{BRRRY'tuple.Item2);BRR}BRRYmtuple.XItem3X)BRR{BRRRY'tuple.Item3);BRR}BR}B}BBXBB1BTrueXBBYDYQYbYJYAB{BRY%R{XBRRY{CY_ four-item tuple.BRRY9Use var implicit type.BRRXvarX tupleYyYqTuple<YP, YP[], Yz, Yz[]>(XVperlVX,BRRRYqYP[] { XVjavaVX, XVc#VX },BRRR1,BRRRYqYz[] { 2, 3 });XBRRY{Pass tuple as argument.BRRXM(tuple);BR}BBRY?YcM(XTupleX<YP, YP[], Yz, Yz[]> tuple)BR{XBRRY{Evaluate the tuple's items.BRRXY'tuple.Item1);BRRY@ (YP YhYptuple.Item2)BRR{BRRRY'Yh);BRR}BRRY'tuple.Item3);BRRY@ (YiYhYptuple.Item4)BRR{BRRRY'Yh);BRR}BR}B}BBXBBperlBjavaBc#B1B2B3XBBYDYQYbYJYAB{BRY%R{BRRXvarX sextuple =BRRRYqXTupleX<Yz, Yz, Yz, YP, YP, YP>(1,BRRR1, 2, XVdotVX, XVnetVX, XVperlsVX);BRRY'sextuple);BR}B}BBXBB(1, 1, 2, dot, net, perls)XNames:XBBA 2-tuple is called a pair.BA 3-tuple is called a triple.BA 4-tuple is called a quadruple.BA 5-tuple is called a quYzuple.BA 6-tuple is called a sextuple.BA 7-tuple is called a septuple.BLarger tuples are called n-tuples.XBBYDYQYbYJYAB{BRY%R{XBRRY{Use Tuple.CY_ Y?mYg.BRRXvarX tupleYyXTuple.CY_X(XVcatVX, 2, true);XBBRRY{Test Yh of YP.BRRXYP YhYytuple.Item1;BRRYmYhYxXVcatVX)BRR{BRRRY'true);BRR}XBBRRY{Test Item2YVItem3.BRRXY'tuple.Item2Yx10);BRRY'!tuple.Item3);XBBRRY{Ya YP representation.BRRXY'tuple);BR}B}BBXBBTrueBFalseBFalseB(cat, 2, True)XOne implementation of Tuple.CY_: .NET 4.0XBBYBY?Tuple<T1> CY_<T1>(T1 item1)B{BRYK YqTuple<T1>(item1);B}XError:XBBProperty or indexer 'YQ.Tuple...Item1'BRcannot be assigned to--it is read-only.XBBYDYQ;BY BYJYAB{BRY%R{BRRYo<Tuple<Yz, YP>> YnYyYqYo<Tuple<Yz, YP>>();BRRYn.YvYqTuple<Yz, YP>(X1X, XVcatVX));BRRYn.YvYqTuple<Yz, YP>(X100X, XVappleVX));BRRYn.YvYqTuple<Yz, YP>(X2X, XVzebraVX));XBBRRY{Use Sort mYg with Comparison delegate.BRRY9Has two parameters; YK comparison of Item2 on each.BRRXYn.XSortX((a, b) => a.Item2.CompareTo(b.Item2))YbRRY@ (var Y8YpYn)BRR{BRRRY'Y8);BRR}BR}B}BBXBB(100, apple)B(1, cat)B(2, zebra)XBBYDYQYbYJYAB{BRY?Tuple<YP, Yz> XNameAndIdX()BR{XBRRY{This mYg YKs multiple Yhs.BRRXYK YqTuple<YP, Yz>(XVSatya NadellaVX, 100);BR}BBRY%R{BRRvar YMYyXNameAndIdX();BRRYP nameYyYM.Item1;BRRYiidYyYM.Item2;XBRRY{Y= the multiple Yhs YKed.BRRXY'name);BRRY'id);BR}B}BBXBBSatya NadellaB100X

&`\\c=73 items in Tuple4-item Tuple6-item tupleTuple.Create methodsorts List of Tuple instancesreturns multiple values