Home > Backend Development > C++ > ValueTuple vs. Tuple in C#: What are the Key Performance and Design Differences?

ValueTuple vs. Tuple in C#: What are the Key Performance and Design Differences?

DDD
Release: 2024-12-31 06:50:10
Original
1045 people have browsed it

ValueTuple vs. Tuple in C#: What are the Key Performance and Design Differences?

Comparing System.ValueTuple to System.Tuple

While researching decompiled C# 7 libraries, one may encounter the use of ValueTuple generics. Understanding the purpose of ValueTuples and their advantages over traditional Tuples is crucial.

Key Differences:

  • Value Type vs. Reference Type: ValueTuples are value types (structs), while Tuples are reference types (classes). Value types allocate data on the stack, reducing GC pressure.
  • Mutability: ValueTuples are mutable, while Tuples are immutable. This distinction affects object equality.
  • Field Access: ValueTuples expose their elements through fields (e.g., Item1), while Tuples use properties.

Rationale for ValueTuples:

Introducing ValueTuples in C# 7 was driven by performance considerations:

  • No Allocation Penalty: As value types, ValueTuples avoid GC overhead.
  • Automatic Equality: Structs inherent optimized equality, making ValueTuples efficient for comparisons.
  • Syntactic Convenience: The language introduced syntax sugar to simplify working with tuples, making their use more intuitive.

Examples:

Demonstrating the usage and benefits of ValueTuples:

public (int sum, int count) DoStuff(IEnumerable<int> values) 
{
    var res = (sum: 0, count: 0);
    foreach (var value in values) 
    { 
        res.sum += value; 
        res.count++; 
    }
    return res;
}
Copy after login

Using decomposing:

var result = DoStuff(Enumerable.Range(0, 10));
Console.WriteLine($&quot;Sum: {result.sum}, Count: {result.count}&quot;);
Copy after login

Or using a fresh variable:

ValueTuple<int, int> valueTuple = DoStuff(Enumerable.Range(0, 10));
Console.WriteLine(string.Format(&quot;Sum: {0}, Count: {1})&quot;, valueTuple.Item1, valueTuple.Item2));
Copy after login

Compiler Implementation:

The compiler generates code that utilizes ValueTuple's fields internally, while providing named argument access for intuitive usage.

Conclusion:

ValueTuples offer significant performance and convenience advantages over traditional Tuples due to their value-type nature, mutability, and syntactic sugar.

The above is the detailed content of ValueTuple vs. Tuple in C#: What are the Key Performance and Design Differences?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template