concept tuple in category c#

This is an excerpt from Manning's book Code like a Pro in C#.
In this section, we look at how the .NET Framework supports tuples (by way of C#), what they are, and how tuples are supported in other platforms. A highly requested feature by the .NET Framework user community, tuples were introduced into the .NET world in 2018 as part of the .NET Core Preview channel (.NET Core is a spinoff of the .NET Framework. We discuss .NET Core in section 2.2). In practical terms, a tuple is a type that can represent a set (or row) of values. Looking at the formal definition as found in set theory, we can say that a tuple is a finite sequence consisting of terms wherein no term can occur twice. So,
<x, y>is a valid tuple, but<x, y, x>is not. This uniqueness rule does not prohibit us from using variables pointing to the same spot in memory in our C# tuples, as they are considered equal but not identical. So, when would we use a tuple? Tuples allow us to return multiple variables out of a method without storing them into a throw-away object.Table 2.2 Comparing the availability of Tuples across platforms
Functionality
.NET
Java
Go
Python
JavaScript
C++
Ruby
R
Haskell
C
Tuples
Yes
No
No
Yes
No
Yes
No
Yes
Yes
No
In C#, the syntax to create a tuple is, for example:
(string, string) myTuple = ("Code C#", "Like A Pro");Because tuples are immutable, they are great at representing a database record. You can mix and match types within a tuple, as long as the declaration matches the assignment. This means that you can make your tuples as lengthy and complicated as you want, as long as you assign the tuple to a set of objects with the same types (and order of types) as your tuple variable. Listing 2.1 has examples of both a valid assignment where the variable and assigned set have the same types (and order) and one where it does not.

This is an excerpt from Manning's book C# in Depth, Fourth Edition.
Anonymous types help only within a single block of code; for example, you can’t use them as method parameters or return types. C# 7 introduced tuples: value types that effectively act to collect variables together. The framework support for these tuples is relatively simple, but additional language support allows the elements of tuples to be named. For example, instead of the preceding anonymous type, you could use the following:
var book = (title: "Lost in the Snow", author: "Holly Webb"); Console.WriteLine(book.title);Tuples can replace anonymous types in some cases but certainly not all. One of their benefits is that they can be used as method parameters and return types. At the moment, I advise that these be kept within the internal API of a program rather than exposed publicly, because tuples represent a simple composition of values rather than encapsulating them. That’s why I still regard them as contributing to simpler code at the implementation level rather than improving overall program design.
C# 7 introduces support for tuples to make data composition simple along with deconstruction from a composite type into its individual components. If you’re now thinking to yourself that C# already has tuples in the form of the System.Tuple types, you’re right to an extent; those types already exist in the framework but don’t have any language support. To add to the confusion, C# 7 doesn’t use those tuple types for its language-supported tuples. It uses a new set of System.ValueTuple types, which you’ll explore in section 11.4. There’s a comparison with System.Tuple in section 11.5.1.
The next sentence may come as a shock to you, so please prepare yourself: tuple types are value types with public, read/write fields. Surely not! I usually recommend against mutable value types in the strongest possible terms, and likewise I always suggest that fields should be private. In general, I stand by those recommendations, but tuples are slightly different.
Listing 11.3. Using a tuple instead of two local variables in MinMax
static (int min, int max) MinMax(IEnumerable<int> source) { using (var iterator = source.GetEnumerator()) { if (!iterator.MoveNext()) { throw new InvalidOperationException( "Cannot find min/max of an empty sequence"); } var result = (min: iterator.Current, #1 max: iterator.Current); #1 while (iterator.MoveNext()) { result.min = Math.Min(result.min, iterator.Current); #2 result.max = Math.Max(result.max, iterator.Current); #2 } return result; #3 } }

This is an excerpt from Manning's book Functional Programming in C#: How to write better C# code.
Better syntax for tuples is the most important feature of C# 7. It allows you to easily create and consume tuples, and, most importantly, to assign meaningful names to their elements. For example, the Stats property returns a tuple of type (double, double), and specifies meaningful names by which its elements can be accessed:
public (double Circumference, double Area) Stats => (Circumference, Area);Tuples are important in FP because of the tendency to break tasks down into very small functions. You may end up with a data type whose only purpose is to capture the information returned by one function, and that’s expected as input by another function. It’s impractical to define dedicated types for such structures, which don’t correspond to meaningful domain abstractions. That’s where tuples come in.
The last example in table 3.1 shows multiple input arguments: we’ll just group them with parentheses (parentheses are used to indicate tuples; that is, we’re notating a binary function as a unary function whose input argument is a binary tuple).
How many possible input values are there? Well, there are two possible values for Gender and 120 for Age, so in total there are 2 * 120 = 240 possible inputs. Notice that if you define a tuple of Age and Gender, 240 tuples are possible. The same is true if you define a custom object to hold that same data, like this:

This is an excerpt from Manning's book Real-World Functional Programming with Examples in F# and C#.
let withItem2 newItem2 tuple = match tuple with | (originalItem1, _) -> (originalItem1, newItem2)The match construct lets us match the specified expression (tuple) against one or more patterns starting with the bar symbol. In our example, we have only one pattern, and because any tuple with two elements can be deconstructed into two values containing its elements, the execution will always follow this single branch. The F# compiler analyzes the pattern matching to deduce that the argument tuple is a tuple type containing two elements.
We’ve been working with tuples from our first functional code in chapter 3, so you’re already quite familiar with them. We haven’t looked at how we can work with them using higher-order functions. Tuples are really simple, so you can often use them directly, but in some cases the code isn’t as concise as it could be. Tuples are a good starting point for exploring higher-order functions because they’re so simple. The principles we’ll see here apply to other types as well.
In chapter 3, we used tuples to represent a city and its population. When we wanted to increment the population, we had to write something like this:
let (name, population) = oldPrague let newPrague = (name, population + 13195)This is very clear but a bit longwinded. The first line deconstructs the tuple, and the second one performs a calculation with the second element and then builds a new tuple. Ideally, we’d like to say that we want to perform a calculation on the second element deconstructing and then reconstructing the tuple. First let’s look at the code we want to be able to write, in both F# and C#, and then we’ll implement the methods that make it all work. This is what we’re aiming for:
let newPrague = oldPrague |> mapSecond ((+) 13195) //F# var newPrague = oldPrague.MapSecond(n => n + 13195); //C#This version removes all the additional code to reconstruct the tuple and specifies the core idea—that is, we want to add some number to the second element from the tuple. The idea that we want to perform a calculation on the second element is expressed by using the mapSecond function in F#. Listing 6.4 shows the implementation of both this and the similar mapFirst function.
Listing 6.4 implements two functions: one that performs an operation on the first element of the tuple ❶ and one that acts on the second element ❷. The implementation of these functions is simple: we use pattern matching in the parameter list to deconstruct the given tuple, then call the function on one of the elements. Finally, we return a new tuple with the result of the function call and the original value of the other element. Even though the body doesn’t look difficult, the inferred type signatures ❸ look rather complicated when you see them for the first time. We’ll come back to them shortly.