C # add that keyword

Source: Internet
Author: User

If the keyword that is added in C #, what does it mean?

A simple analysis shows that C # adds the that keyword to correspond to or at least the level of this keyword. Therefore, we should try to maintain the same level in terms of syntax and semantics. That is to say, the added that keyword should also be used to indicate an implicit object reference. Similarly, there are base keywords, which are all affiliated with access keywords.

Therefore, the opinion of Matthew W. Jackson and anony is to apply this keyword to the implicit context reference of the with statement, as shown in

 
  
  
  1. Public int thatexample <t> (TX, Ty)
  2. {
  3. With (system. Collections. comparer <int>. Default)
  4. {
  5. If (that. Compare (x, y)> 0)
  6. Return that. gethashcode (X );
  7. Else
  8. Return that. gethashcode (y );
  9. }
  10. }

The advantage is that it can clearly obtain the implicit object reference within the with range, and the semantics is similar to the implicit semantics of this. At the same time, it can provide the ability to pass the implicitly referenced object as a parameter to other methods within the with range, which is not available in the with syntax such as Pascal.

  
  
  1. Withnewfoo ()
  2. {
  3. Whateverproperty = "property ";
  4. Intproperty = 1;
  5. // Callwhateverwiththefoowejustmade
  6. // Beforecallingenteringtheblock
  7. Bar. Whatever (that );
  8. }

In addition to the implicit object access feature in the scope of the object, the this keyword actually has a feature passed in by implicit parameters. That is to say, when the object method is called, the reference of the object itself is passed in to the method implicitly through this. The result keyword in the number Pascal corresponding to this feature explicitly accesses the return value of the function. Therefore, we recommend that you use the that keyword as follows:

 
  
  
  1. Public int thatexample<T>(TX, T, Y)
  2. {
  3. Icomparer<T>Comparer = comparer<T>. Default;
  4. If (comparer. Compare (x, y)>0)
  5. That = comparer. gethashcode (X );
  6. Else
  7. That = comparer. gethashcode (y );
  8. }

In this way, you do not need to manually construct the return value instance, but the compiler takes on this note. This is also a good compiler hint, which can be used to avoid unnecessary temporary variables during JIT optimization, it would be nice if the C ++ Value-Based Semantic system could add this feature.

In addition, it is recommended that you use this for the purposes opposite to this, such as pointing to another object of the binary operator in Operator overloading, avoiding the use of variables such as RHS, which is implicitly processed by the compiler, reduce notes.

Or for some special purposes, such as pointing to the target object of the clone operation in the clone function

 
  
  
  1. Lassx: icloneable
  2. {
  3. Private someotherdeepcopyableclass data;
  4. [Usescopy]
  5. Public override object clone ()
  6. {
  7. That. Data = (someotherdeepcopyableclass) data. Clone ();
  8. }
  9. }

Alternatively, you can use foreach as a qualifier to filter data. Similar to the limitations in C #2.0 for generic support, the above section describes how to add the "that" keyword to C.

 

Address: http://www.csharpwin.com/csharpspace/7346r469.shtml#PageTop

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.