introduce

A Tuple

What Tuple does:

Store a set of data

You can make a method return multiple values without using the OUT argument

In the.net Framework, Tuple was introduced before 4.7, and ValueTuple was introduced after 4.7

Tuples existed in.net Core since version 1.0, and ValueTuples were introduced in 2.0

If it is earlier than.net Framework 4.7 or. To use ValueTuple before asp.net Core2.0, you can install system.valuetuple via NuGet

Tuple is new in C#4.0 and ValueTuple is new in C#7.0

The basic difference between a Tuple and a ValueTuple is that a Tuple is a class and a ValueTuple is a struct

ValueTuple is more powerful than Tuple

Let’s look at the basic usage of Tuple and ValueTuple

usage

Tuple

Creating a tuple

Var a = new Tuple<int, string>(1001, "Jack"); Var b = Tuple.Create(1002, "Tom");Copy the code

A Tuple can create up to 8 elements. You can define tuples within elements

Accessing tuple data

Tuple data can be accessed through Item1, Item2

Var id = a.item1; var name = a.Item2;Copy the code

The tuple is the return value of the method

public Tuple<int, string> GetUserInfo()
{
    return Tuple.Create(1001, "Jack");
}
Copy the code

Receive data

var user = GetUserInfo();
Copy the code

The Tuple function is weak

1. Elements can only be read, but cannot be modified

2. You can only access the element as Item1. You cannot define the element name

ValueTuple will solve the problem of the above Tuple

ValueTuple

Creating a tuple

There is a Tuple creation method similar to the one above

var b = new ValueTuple<int, string>(1001, "Jack");
var d = ValueTuple.Create(1002, "Tom");
Copy the code

It can also be created by (,) to make the code more concise

ValueTuple<int,string> userInfo1 = (1003, "Mark");
(int, string) userInfo2 = (1003, "Mark");
var userInfo3 = (1003, "Mark");
Copy the code

Give the element a name when you define it

var userInfo1 = (id: 1003, name: "Mark");

(int id, string name) userInfo2 = (1003, "Mark");
Copy the code

Accessing tuple data

Also access tuple data through Item1, Item2

A ValueTuple has the advantage of being able to define the name of an element and give it a meaningful name

 (int id, string name) userInfo = (1003, "Mark");
Copy the code

This allows access to the data by id and name

ValueTuple can not only access data, but also modify data

(int id, string name) userInfo = (1003, "Mark");
userInfo.id = 1010;
userInfo.name = "haha"; 
Copy the code

The tuple is the return value of the method

It can be

public ValueTuple<int, string> GetUserInfo()
{
    return ValueTuple.Create(1003, "Mark");
}
Copy the code

Or you can abbreviate it

public (int, string) GetUserInfo2()
{
    return (1003, "Mark");
}
Copy the code

Receive data

Can be received by defining a tuple

 (int, string) userInfo2 = GetUserInfo2();
Copy the code

It can also be received through var

var userInfo3 = GetUserInfo2();
Copy the code

Of course, you can easily access the data by defining tuples and element names

(int id, string name) userInfo4 = GetUserInfo2();

var uid = userInfo4.id;
var uname = userInfo4.name;
Copy the code

This can also define the element name in the return value of the method

public (int id, string name) GetUserInfo2()
{
    return (1003, "Mark");
}
Copy the code

Define two variables to receive data

(int id, string name)  = GetUserInfo2();

var uid = id;
var uname = name;
Copy the code

This is different from the above definition, where we define two variables

// Define a ValueTuple (int ID, string name) userInfo1 = (1003, "Mark"); var uid = userInfo1.id; var uname = userInfo1.name; // Define two variables (int id, string name) = (1003, "Mark"); var uid2 = id; var uname2 = name;Copy the code

Tuple compatibility problem

The compatibility of tuples means that the elements of tuples are of the same type and order, regardless of their names

conclusion

ValueTuple has all the functions that a Tuple has, so ValueTuple is usually used

ValueTuple uses (,) to simplify the code

ValueTuple can read and write, which is more useful than Tuple

ValueTuple supports element naming for easy use

ValueTuple can be used as the return value of a method, returning multiple values at once without redefining the class as an argument or using an out argument