A detailed description of C # Tuple vs ValueTuple (tuple class vs value tuple)

Source: Internet
Author: User

C # 7.0 has been out for some time, and we all know that there is a tuple optimization in the new feature: ValueTuple. Here's a detailed example of tuple vs ValueTuple (tuple class vs value tuple), 10 minutes to get to know the benefits and usage of ValueTuple.

If you know enough about the tuple, you can skip the section "Review tuple" and go directly to the section "Valuetuple details" to see the dazzle usage of value tuples.

Review of tuple

A tuple is a new feature of C # 4.0, and the. Net Framework version 4.0 is available.

A tuple is a data structure that has a specific number and sequence of elements. For example, design a ternary data structure for storing student information, a total of three elements, the first is the name, the second is the age, the third is the height.

The specific use of tuples is as follows:

1. How to create a tuple

By default,. Net Framework tuples support only 1 to 7 tuple elements, and if you have 8 elements or more, you need to use the nested and rest attributes of a tuple to implement them. In addition, the tuple class provides static methods for creating tuple objects.

    • To create a tuple with a constructor:
var testTuple6 = new Tuple<int, int, int, int, int, int> (1, 2, 3, 4, 5, 6); Console.WriteLine ($ "Item 1: {TESTTUPLE6.ITEM1}, item 6: {TESTTUPLE6.ITEM6}"), var testTuple10 = new Tuple<int, int, int , int, int, int, int, tuple<int, int, int>> (1, 2, 3, 4, 5, 6, 7, new tuple<int, int, int> (8, 9, 10)); Console.WriteLine ($ "Item 1: {TESTTUPLE10.ITEM1}, item: {TESTTUPLE10.REST.ITEM3}");
    • A tuple static method is used to build a tuple with up to eight elements supported:
var testTuple6 = tuple.create<int, int, int, int, int, int> (1, 2, 3, 4, 5, 6); Console.WriteLine ($ "Item 1: {TESTTUPLE6.ITEM1}, item 6: {TESTTUPLE6.ITEM6}"), var testTuple8 = tuple.create<int, int, int, int, int, int, int, int> (1, 2, 3, 4, 5, 6, 7, 8); Console.WriteLine ($ "Item 1: {TESTTUPLE8.ITEM1}, item 8: {TESTTUPLE8.REST.ITEM1}");

Note: The tuple type that is constructed here is actually tuple<int, int, int, int, int, int, int, tuple<int>>, and so the data type that testtuple8.rest takes is a tuple <int>, so you need to take the Item1 attribute to get the exact value.

2. Represents a set of data

Create a tuple as follows three messages for a student: name, age, and height, instead of creating a separate class.

var studentinfo = tuple.create<string, int, uint> ("Bob", 28, 175); Console.WriteLine ($ "Student information:name [{studentinfo.item1}], age [{studentinfo.item2}], Height [{ STUDENTINFO.ITEM3}] ");
3. Returning multiple values from a method

When a function needs to return multiple values, it is generally possible to use an out parameter, where a tuple can be used instead of the out implementation to return multiple values.

static tuple<string, int, uint> getstudentinfo (string name) {    return new tuple<string, int, uint> ("Bob", 28, 175);}
static void Runtest () { var studentinfo = getstudentinfo ("Bob"); Console.WriteLine ($ "Student information:name [{studentinfo.item1}], age [{studentinfo.item2}], Height [{ STUDENTINFO.ITEM3}] ");

4. Multi-value transfer for single-parameter methods

When a function parameter is only an object type, you can pass multiple parameter values using a tuple implementation.

static void Writestudentinfo (Object student) {    var studentinfo = student as tuple<string, int, uint>;    Console.WriteLine ($ "Student information:name [{studentinfo.item1}], age [{studentinfo.item2}], Height [{ STUDENTINFO.ITEM3}] ");
static void Runtest () { var t = new System.Threading.Thread (New System.Threading.ParameterizedThreadStart ( Writestudentinfo)); T.start (new tuple<string, int, uint> ("Bob", page, 175)); while (t.isalive) { System.Threading.Thread.Sleep ()} }

Although tuples have the easy-to-use methods described above, they also have significant deficiencies:

    • Access to the elements can only be accessed through the ITEMX, the need to clear the order of elements before use, the property name has no practical significance, inconvenient memory;
    • There are up to eight elements, which can only be nested by the last element.
    • A tuple is a reference type that, unlike other simple types, is a value type that allocates space on the heap and may have too many creation and allocation work when CPU-intensive operations occur.

As a result, a new valuetuple type is introduced in C # 7.0, as described in the following sections.

Valuetuple detailed

Valuetuple is one of the new features of C # 7.0, which is available in the. Net Framework version 4.7.

A value tuple is also a data structure that represents a specific number and sequence of elements, but is not the same as a tuple class, and the main differences are as follows:

    • A value tuple is a struct, a value type, not a class, whereas a tuple (tuple) is a class, a reference type;
    • The value tuple element is variable, not read-only, which means that the value of the element in the value tuple can be changed;
    • The data member of a value tuple is a field that is not a property.

The specific use of value tuples is as follows:

1. How to create value tuples

Like a tuple class, a. Net framework value tuple also supports only 1 to 7 tuple elements, and if there are 8 elements or more, it needs to be implemented using the nested and rest attributes of the value tuple. In addition, the Valuetuple class can provide static methods for creating value tuple objects.

    • To create a tuple with a constructor:
var testTuple6 = new Valuetuple<int, int, int, int, int, int> (1, 2, 3, 4, 5, 6); Console.WriteLine ($ "Item 1: {TESTTUPLE6.ITEM1}, item 6: {TESTTUPLE6.ITEM6}"); var testTuple10 = new Valuetuple<int, int, int, int, int, int, int, valuetuple<int, int, int>> (1, 2, 3, 4, 5, 6, 7, new ValueTuple <int, int, int> (8, 9, 10)); Console.WriteLine ($ "Item 1: {TESTTUPLE10.ITEM1}, item: {TESTTUPLE10.REST.ITEM3}");
    • A tuple static method is used to build a tuple with up to eight elements supported:
var testTuple6 = valuetuple.create<int, int, int, int, int, int> (1, 2, 3, 4, 5, 6); Console.WriteLine ($ "Item 1: {TESTTUPLE6.ITEM1}, item 6: {TESTTUPLE6.ITEM6}"); var testTuple8 = valuetuple.create<int, int, int, int, int, int, int, int> (1, 2, 3, 4, 5, 6, 7, 8); Console.WriteLine ($ "Item 1: {TESTTUPLE8.ITEM1}, item 8: {TESTTUPLE8.REST.ITEM1}");

Note that the tuple type that is constructed here is actually tuple<int, int, int, int, int, int, int, tuple<int>>, and so the data type that testtuple8.rest takes is a tuple <int>, so you need to take the Item1 attribute to get the exact value.

Optimization Difference : When you construct a value tuple above 7 elements, you can use the next itemx to access the values in the nested tuple, for the above example, to access the tenth element, It can be accessed either through testTuple10.Rest.Item3 or through TESTTUPLE10.ITEM10.

var testTuple10 = new Valuetuple<int, int, int, int, int, int, int, valuetuple<int, int, int>> (1, 2, 3, 4, 5, 6, 7, new valuetuple<int, int, int> (8, 9, 10)); Console.WriteLine ($ "Item: {TESTTUPLE10.REST.ITEM3}, item: {TESTTUPLE10.ITEM10}");
2. Represents a set of data

Create a value tuple as follows three information for a student: First name, age, and height, instead of creating a separate class.

var studentinfo = valuetuple.create<string, int, uint> ("Bob", 28, 175); Console.WriteLine ($ "Student information:name [{studentinfo.item1}], age [{studentinfo.item2}], Height [{ STUDENTINFO.ITEM3}] ");
3. Returning multiple values from a method

Value tuples can also return multiple values in the function definition instead of out parameters.

static valuetuple<string, int, uint> getstudentinfo (string name) {    return new ValueTuple <string, int, uint& gt; ("Bob", 28, 175);}
static void Runtest () { var studentinfo = getstudentinfo ("Bob"); Console.WriteLine ($ "Student information:name [{studentinfo.item1}], age [{studentinfo.item2}], Height [{ STUDENTINFO.ITEM3}] ");

Optimization Difference : The return value can be unspecified valuetuple, using the new syntax (,,) instead, such as (string, int, uint):

static (string, int, uint) GetStudentInfo1 (string name) {    return ("Bob", 28, 175);}
static void RunTest1 () { var studentinfo = GetStudentInfo1 ("Bob"); Console.WriteLine ($ "Student information:name [{studentinfo.item1}], age [{studentinfo.item2}], Height [{ STUDENTINFO.ITEM3}] ");

The type of Debug View Studentinfo is the ValueType ternary group.

Optimization Difference : The return value can specify the element name, easy to understand memory assignment and access:

Static (string name, int age, uint height) GetStudentInfo1 (string name) {    return ("Bob", 28, 175);}
static void RunTest1 () { var studentinfo = GetStudentInfo1 ("Bob"); Console.WriteLine ($ "Student information:name [{studentinfo.name}], age [{studentinfo.age}], Height [{ Studentinfo.height}] ");

Convenient memory Assignment:

Easy access:

4. Multi-value transfer for single-parameter methods

When a function parameter is only an object type, you can pass multiple values using a value tuple implementation.

static void Writestudentinfo (Object student) {    var studentinfo = (valuetuple<string, int, uint>) student;    Console.WriteLine ($ "Student information:name [{studentinfo.item1}], age [{studentinfo.item2}], Height [{ STUDENTINFO.ITEM3}] ");
static void Runtest () { var t = new System.Threading.Thread (New System.Threading.ParameterizedThreadStart ( Writestudentinfo)); T.start (new valuetuple<string, int, uint> ("Bob", page, 175)); while (t.isalive) { System.Threading.Thread.Sleep ()} }
5. Deconstruction Valuetuple

You can construct local variables by using VAR (x, Y) or (Var x, var y) to parse value tuple elements, and you can use the symbol "_" to ignore unwanted elements.

Static (string name, int age, uint height) GetStudentInfo1 (string name) {    return ("Bob", 28, 175);} static void RunTest1 () {    var (name, age, height) = GetStudentInfo1 ("Bob");    Console.WriteLine ($ "Student information:name [{Name}], age [{age}], Height [{Height}]");    (Var name1, var age1, var height1) = GetStudentInfo1 ("Bob");    Console.WriteLine ($ "Student information:name [{name1}], age [{age1}], Height [{height1}]");    var (_, Age2, _) = GetStudentInfo1 ("Bob");    Console.WriteLine ($ "Student information:age [{age2}]");}

Valuetuple makes C # easier to use, as described above. The main benefits compared to the tuple are as follows:

    • Valuetuple supports function return value new Syntax "(,,)" to make the code simpler;
    • The ability to name the elements, easy to use and memory, it is important to note that although named, but in fact, the value tuple does not define the name of the property or field, the real name is still itemx, all the element names are only designed and compiled, Not run-time (so be aware of serialization and deserialization of the type);
    • You can use the Deconstruction method to make it easier to use elements of some or all tuples;
    • Value tuples are value types and are used more efficiently than tuples of reference types, and value tuples have a comparison method and can be used to compare equality, see: Https://msdn.microsoft.com/en-us/library/system.valuetuple.

[Original articles, reproduced please indicate the source, only for study purposes, if there are errors please leave a message, such as feel good, please recommend, thank you support]

[Original: Http://www.cnblogs.com/lavender000/p/6916157.html, from forever-smoked]

A detailed description of C # Tuple vs ValueTuple (tuple class vs value tuple)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.