C # 7.0

Source: Internet
Author: User

C # 7.0

This article refers to the issue: #118 in the Roslyn project.

  1. C # 7.0 new Feature 1: tuple-based "multiple" return value method

2. C # 7.0 new Feature 2: Local method

3. C # 7.0 new Feature 3: pattern matching

4. C # 7.0 new Feature 4: return reference

C # Early in the initial release of C # 1.0 (January 2002), the reference and continuation of pointer parameters in C + +, native allows a reference (pointer) of the value type data to be passed to the method body in the form of a tag ref parameter.

But for a value type reference within a method, how to return it as a reference, there has been no perfect solution, although this use case is rare.

To raise a simple question, we need to get a reference to the maximum value in three int .

We look at the practice before c#7.0:

C + + pointers

We're back in C-+ +, and there's nothing controversial about this, and it's very well-deserved to realize this:

1 int* Max (int* first, int* second, int* third) {2   int* Max = *first > *second? first:second;3   return *max & Gt *third? Max:third;4}5 .... 6 int a = 1, b = 2, c = 3;7 int* max = max (&a, &b, &c); 8 *max = 4; c = = 4;

Let's consider how to translate this code reasonably in C #.

/UNSAFE directive

There may be children's shoes to see C + + pointers, already thought out. NET compiler directive, open the /unsafe directive, which allows C # to access memory directly. Indeed, just tick "allowunsafe code" in the project.

You can do this in almost the same way as in C + +:

1 unsafe static int* Max (int* first, int* second, int* third) 2 {3     int* Max = *first > *second? first:second; 4     return *max > *third max:third; 5} 6 .... 7 int a = 1, b = 2, c = 3; 8 unsafe 9 {     int* max = max (&a, &b, &c);     *max = 4;//c = = 412}

However, unsafe is not recommended by C #, it bypasses the CLR's memory security mechanism, the pointer's unsafe misuse is allowed, and it's easy to point your pointer to a variety of unintended targets, such as allowing access to the call stack that has been returned (released), Let's do an experiment.

1 unsafe static int* GetRef () 2 {3     //some codes 4     int i = 4; 5     return &i; 6} 7 unsafe static void Main (s Tring[] args) 8 {9     int* num = GetRef ();     Console.WriteLine (*num);//411     //some codes12     Console.WriteLine (*num); Not expected 13}

This is a very typical error when the call stack of GETREF () is returned after it is released, and we try to get its local reference (num) If GetRef is left in the memory of the stack structure the fluke is not reassigned and we can still get it.

But normally, once our logic needs to do some other processing (including the first Console.WriteLine () call itself ), theunsafe memory that NUM resides in will naturally be overwritten.

While this is a code of its own error, it stands at the language level and does nothing to circumvent anything that can be done completely. (This problem is also present in C + +)

Returning Model objects

Of course, in fact, c#6.0 and before, we have a more common scenario: the need to return a reference value type encapsulated in a hosted model class.

Because the object is passed with the address of the reference heap, the reference target is not on the call stack, and is not released because the function returned.

1 static Hostmodel Max (Hostmodel first, Hostmodel second, Hostmodel third) 2 {3     Hostmodel max = first. Value > second. Value? First:second;4     return max. Value > third. Value? MAX:THIRD;5}

This kind of similar practice is widely used in model transmission, DTOs and other scenes, it is understandable.

However, in scenarios where performance requirements are sensitive and the data and logical structure are simple , it is wasteful and extravagant to have a single set of boxing and unboxing actions for a simple data that is not necessary in the heap in the form of objects.

Reference returns

The concept of a reference return (refreturn) is introduced in c#7.0, which allows a C # method to return a reference to a value type.

Issue: #118. The following example is given in:

1 static ref int MAX (ref int first, ref int second, ref int third) 2 {3     ref int max = first > second? ref first: Ref second; 4     return max > Third ref max:ref third; 5} 6 ... 7 int a = 1, b = 2, c = 3; 8 Max (ref A, ref B, ref c) = 4; 9 Debug.Assert (A = = 1); True10 Debug.Assert (b = = 2); True11 Debug.Assert (c = = 4); True

In this way, we can directly return the reference on the call stack via c#7.0.

Also, for larger structures (structs), it is much faster to return a reference than to pass the structure value because the assignment of the struct copies the entire structure.

It is also important to note that ref return references, at the language level, attach rules that do not allow a reference to a local variable within a method, in other words, the stack address that is returned must be below the entry address of the current method.

Summarize

We look at this from the other side of the feature, in fact, the performance requirements of the extreme circumstances of the consideration, for the current majority. NET application, in fact, use cases are very limited, not in the past. NET-focused aspects:

But does the Roslyn project include this feature in the early stages of c#7.0 design, and does it imply a longer-term consideration?

It's not hard to see the latest news from Microsoft, Satya Nadella announced the creation of an Internet of things lab at a developer summit in Beijing earlier this month (June 1), and Microsoft's IoT suite was released at the summit.

Recently Microsoft has also released the IoT version of Windows (Windows IoT), and the recently released. NET core is also allowed to run on devices such as the Raspberry PI (Raspberry Pi), which is equipped with Windows IoT.

In these memory, such as gold, the end of the device, C # want to have a seat, the inevitable need to change the memory of the past some of the wayward design, it can be understood. This may be an important reason for c#7.0 to join ref return.

This article link: http://www.cnblogs.com/ylvict/p/5633480.html (reprint please specify)

C # 7.0

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.