Vernacular C # tuples of new syntax features,

Source: Internet
Author: User

Vernacular C # tuples of new syntax features,

1. Tuple)

Tuple comes in 4.0, but it also has some disadvantages, such:

1) Tuple affects code readability because its attribute names are Item1, Item2...

2) Tuple is not lightweight enough because it is a reference type (Class). It is unnecessary to use a type for a variable.

The source code is as follows:

1 // 2 // Summary: 3 // provides a static method for creating a tuples object. To browse the Source code of this type of. NET Framework, see Reference Source. 4 public static class Tuple 5 {6 // return result: 7 // a Tuple with its value (item1 ). 8 public static Tuple <T1> Create <T1> (T1 item1); 9 10 // return result: 11 // a 2-tuples with values (item1, item2 ). 12 public static Tuple <T1, T2> Create <T1, T2> (T1 item1, T2 item2); 13}
Provides static methods for creating tuples.

 

Note: The Tuple mentioned above is not lightweight enough. In a sense, it is a hypothesis that there are many Allocation Operations.

The ValueTuple in C #7 solves the preceding two disadvantages:

1) ValueTuple supports semantic field naming. You can customize the name for each attribute name. For example, (int first, int second) tuple = ).

2) ValueTuple is a value type (Struct ).

NOTE: If vs reports an error that does not have a pre-defined ValueTuple <...>, use the nuget command to import: Install-Package System. ValueTuple

The source code is as follows (ValueTuple <...> internal principle ):

 1     public struct ValueTuple<T1, T2> : IEquatable<ValueTuple<T1, T2>>, IStructuralEquatable, IStructuralComparable, IComparable, IComparable<ValueTuple<T1, T2>>, ITupleInternal 2     { 3         public T1 Item1; 4         public T2 Item2; 5         int ITupleInternal.Size 6         { 7             get 8             { 9                 return 2;10             }11         }12         public ValueTuple(T1 item1, T2 item2)13         {14             this.Item1 = item1;15             this.Item2 = item2;16         }17          }
ValueTuple (structure)

Summary: The appearance of tuples simplifies object orientation to a certain extent. Some unnecessary or rarely used objects can be directly returned using tuples without returning them by type.

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.