DQbJA%{_zPqzP{mx'mx'm'DQbJA%{_9yqPPzzqPqz{?cPPzz{'@Php'h'@ihp'hDQbJA%qzzzPPP'zDQbJA%{_?gy_{hPPhymhx'{V'x'{aP'_B?_KqQDQ JA%ozPnyqozPnvqzPnvqzPnvqzP{g9Knb@8pn'8DQbJA?Pz{gKhKqPz%MyPyMiyM{=hK''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.

XYZ YYYYY { XYX{Z XXYCY three-item tuple. XXZTupleZ<Y, Y, bool> tuple = XXXYTuple<Y, Y, bool>(Z1Z, Z"cat"Z, ZtrueZ);Z XXYAccess tuple properties. XXZYtuple.ZItem1ZY1) XX{ XXXYtuple.Item1); XX} XXYtuple.ZItem2ZYZ"dog"Z) XX{ XXXYtuple.Item2); XX} XXYtuple.ZItem3Z) XX{ XXXYtuple.Item3); XX} X} } Z 1 TrueZ YYYYY { XYX{Z XXYCY four-item tuple. XXYUse var implicit type. XXZvarZ tupleYYTuple<Y, Y[], Y, Y[]>(Z"perl"Z, XXXYY[] { Z"java"Z, Z"c#"Z }, XXX1, XXXYY[] { 2, 3 });Z XXYPass tuple as argument. XXZM(tuple); X} XYYM(ZTupleZ<Y, Y[], Y, Y[]> tuple) X{Z XXYEvaluate the tuple's items. XXZYtuple.Item1); XXY (Y YYtuple.Item2) XX{ XXXYY); XX} XXYtuple.Item3); XXY (YYYtuple.Item4) XX{ XXXYY); XX} X} } Z perl java c# 1 2 3Z YYYYY { XYX{ XXZvarZ sextuple = XXXYZTupleZ<Y, Y, Y, Y, Y, Y>(1, XXX1, 2, Z"dot"Z, Z"net"Z, Z"perls"Z); XXYsextuple); X} } Z (1, 1, 2, dot, net, perls)ZNames:Z A 2-tuple is called a pair. A 3-tuple is called a triple. A 4-tuple is called a quadruple. A 5-tuple is called a quYuple. A 6-tuple is called a sextuple. A 7-tuple is called a septuple. Larger tuples are called n-tuples.Z YYYYY { XYX{Z XXYUse Tuple.CY YmY. XXZvarZ tupleYZTuple.CYZ(Z"cat"Z, 2, true);Z XXYTest Y of Y. XXZY YYtuple.Item1; XXYYYZ"cat"Z) XX{ XXXYtrue); XX}Z XXYTest Item2YItem3. XXZYtuple.Item2Y10); XXY!tuple.Item3);Z XXYY Y representation. XXZYtuple); X} } Z True False False (cat, 2, True)ZOne implementation of Tuple.CY: .NET 4.0Z YYTuple<T1> CY<T1>(T1 item1) { XY YTuple<T1>(item1); }ZError:Z Property or indexer 'Y.Tuple...Item1' Xcannot be assigned to--it is read-only.Z YY; Y YY { XYX{ XXY<Tuple<Y, Y>> YYYY<Tuple<Y, Y>>(); XXY.YYTuple<Y, Y>(Z1Z, Z"cat"Z)); XXY.YYTuple<Y, Y>(Z100Z, Z"apple"Z)); XXY.YYTuple<Y, Y>(Z2Z, Z"zebra"Z));Z XXYUse Sort mY with Comparison delegate. XXYHas two parameters; Y comparison of Item2 on each. XXZY.ZSortZ((a, b) => a.Item2.CompareTo(b.Item2))YXXY (var YYY) XX{ XXXYY); XX} X} } Z (100, apple) (1, cat) (2, zebra)Z YYYYY { XYTuple<Y, Y> ZNameAndIdZ() X{Z XXYThis mY Ys multiple Ys. XXZY YTuple<Y, Y>(Z"Satya Nadella"Z, 100); X} XYX{ XXvar YYZNameAndIdZ(); XXY nameYY.Item1; XXYidYY.Item2;Z XXYY the multiple Ys Yed. XXZYname); XXYid); X} } Z Satya Nadella 100Z

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