This article is a mirror article of machine translation, please click here to jump to the original article.

View: 24117|Reply: 1

[Source] Detailed explanation of C# Tuple VS ValueTuple

[Copy link]
Posted on 5/14/2018 4:37:48 PM | | | |
C# 7.0 has been out for a while, and everyone knows that there is an optimization for tuples in the new feature: ValueTuple. Here are detailed examples to explain Tuple VS ValueTuple in 10 minutes to better understand the benefits and usage of ValueTuple.

If you know enough about Tuple, you can skip the section "Review Tuple" and go straight to the section "ValueTuple Explained" to see the dazzling usage of value tuples.



Look back at Tuple

Tuple is a new feature that came out with C# 4.0 and is available in .Net Framework 4.0 and above.

A tuple is a data structure with a specific number and sequence of elements. For example, design a triple data structure to store student information, which contains a total of three elements, the first is the name, the second is the age, and the third is the height.

The specific uses of tuples are as follows:

1. How to create a tuple

Default. Net FrameworkTuples only support 1 to 7 tuple elementsIf there are 8 elements or more, you need to use Tuple's nesting and Rest properties to implement it. In addition, the Tuple class provides a static method for creating tuple objects.

Create tuples using constructors:

Construct tuples using the Tuple static method, supporting up to eight elements:
Note: The Tuple type constructed here is actually Tuple<int, int, int, int, Tuple<int>>, so the data type obtained by testTuple8.Rest is Tuple<int>, so to get the exact value, you need to take the Item1 property.

2. Represents a set of data
Create a tuple below to represent three pieces of information about a student: name, age, and height, without creating an additional class separately.

3. Return multiple values from a method
When a function needs to return multiple values, the out parameter can generally be used, and tuples can be used instead of out to return multiple values.
4. Multi-value transfer for single-parameter methods

When the function parameter is only one Object type, multiple parameter values can be passed using the tuple implementation.

Despite the above-mentioned handy methods, tuples also have significant shortcomings:

  • When accessing elements, you can only access them through ItemX, and you need to clarify the order of elements before use, and the attribute names have no practical meaning, which is inconvenient to remember.
  • There are up to eight elements, and if you want more, you can only expand by nesting with the last element;
  • Tuple is a reference type, not a value type like other simple types, which allocates space on the heap and can have too much creation and allocation work when CPU intensive.


Therefore, a new ValueTuple type was introduced in C# 7.0, which is detailed in the following sections.

ValueTuple explains in detail

ValueTuple is one of the new features of C# 7.0, available for .Net Framework 4.7 and above.

ValueTuple does not require 4.7, as long as nuget is fine, 4.7 is built-in.
In addition, ValueTuple is a structure type, Microsoft recommends not using structures over 64 bytes, which is also effective for ValueTuple.


A value tuple is also a data structure used to represent a specific number and sequence of elements, but it is different from a tuple class, with the following main differences:

A value tuple is a structure, a value type, not a class, while a tuple is a class, a reference type;
Value tuple elements are variable, not read-only, that is, they can change the value of the element in the value tuple;
A value tuple of data members is a field, not an attribute.
The specific use of value tuples is as follows:

1. How to create a value tuple
Like tuple classes, .Net Framework value tuples only support 1 to 7 tuple elements, and if there are 8 elements or more, you need to use the nested and Rest properties of the value tuple to implement it. In addition, the ValueTuple class can provide static methods for creating value tuple objects.

Create tuples using constructors:

Construct tuples using the Tuple static method, supporting up to eight elements:
Note that the Tuple type constructed here is actually Tuple<int, int, <int>>, so the data type obtained by testTuple8.Rest is Tuple<int>, so to get the accurate value, you need to take the Item1 property.

Optimization Difference:When a tuple of values with more than 7 elements is constructed, the values in the nested tuple can be accessed using the following ItemX, and for the example above, to access the tenth element, either through testTuple10.Rest.Item3 or testTuple10.Item10.
2. Represents a set of data
Create a tuple of values to represent three pieces of information about a student: name, age, and height without creating an additional class separately.
3. Return multiple values from a method
Value tuples can also return multiple values in place of the out parameter in the function definition.
Optimization Difference:The return value can be specified in the ValueTuple inexplicably, using a new syntax (,,) instead, such as (string, int, uint):
Debugging the type of studentInfo is the ValueType triplet.

Optimization Difference:The return value can specify the element name to facilitate understanding memory assignment and access:

Convenient memory assignment:



4. Multi-value transfer for single-parameter methods
When a function parameter is only one Object type, multiple values can be passed using the value tuple implementation.
5. Deconstruct the ValueTuple
You can use var (x, y) or (var x, var y) to resolve tuple elements to construct local variables, while using the symbol "_" to ignore unwanted elements.
As mentioned above, ValueTuple makes C# simpler and easier to use. The main advantages over Tuple are as follows:

  • ValueTuple supports a new syntax for function return values" (,,)", making the code simpler;
  • It should be noted here that although it is named, but in fact the value tuple does not define the attribute or field of such a name, the real name is still ItemX, and all element names are only used during design and compilation, not at runtime (so pay attention to the serialization and deserialization operations of this type);
  • Some or all tuples of elements can be used more conveniently using destruction methods;
  • Value tuples are value types, which are more efficient to use than tuples of reference types, and value tuples have a comparison method that can be used to compare whether they are equal, see Details:https://msdn.microsoft.com/en-us/library/system.valuetuple






Previous:Science popularization tools for large-scale architectures
Next:HUAWEI CLOUD Releases Blockchain White Paper to Accelerate the Implementation of Blockchain Technology Industry
Posted on 5/15/2018 11:41:00 AM |
I agree!
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com