C # code reconstruction using model and extension methods, cf

Source: Internet
Author: User

C # code reconstruction using model and extension methods, cf
C # code reconstruction using model and extension methods

It is often seen in some C # code

Java code  
  1. // An Simple Example By Ray Linn
  2. Class CarCollection: ICollection
  3. {
  4. IList list;
  5. Public void Add (Car car)
  6. {
  7. List. Add (car );
  8. }
  9. ... Function list for ICollection...
  10. Public void listPrice ()
  11. {
  12. Foreach (Car car in list)
  13. System. Console. WriteLin (car. Price );
  14. }
  15. ... More specifical function list...
  16. }
  17. Class PetCollection: ICollection
  18. {
  19. IList list;
  20. Public void Add (Pet pet)
  21. {
  22. List. Add (pet );
  23. }
  24. ... Function list for ICollection...
  25. Public void FeedPet ()
  26. {
  27. Foreach (Pet pet in list)
  28. System. Console. WriteLin (pet. Eating ());
  29. }
  30. ... More specifical function list...
  31. }



Such code is often seen in many Open Source projects, such as Cecil. Its common features are: a specific type of Collection + special operations on the Collection, A project may be filled with dozens of similar collections. Similar code is difficult to refactor in Java, but in C, but the code can be reduced by using the extension method and model.

First, create a Collection of dimensions. This example can be replaced by List <T>, but as an example, we assume that the List <T> is special (there may be some delegate)
Java code

Java code  
  1. Public CommonCollection <T>: ICollection <T>
  2. {
  3. IList <T> list
  4. ... Function list for ICollection...
  5. }
  6. Public CommonCollection <T>: ICollection <T>
  7. {
  8. IList <T> list
  9. ... Function list for ICollection...
  10. }



For special Car and Pet operations, we use the extension method to implement

Java code  
  1. Public static class CarExt
  2. {
  3. // Ext Function For CommonCollection <Car> by Ray Linn
  4. Public static void listPrice (this CommonCollection <Car> collection)
  5. {
  6. Foreach (Car car in collection)
  7. System. Console. WriteLin (car. Price );
  8. }
  9. ... More specifical function list...
  10. }
  11. Public static class PetExt
  12. {
  13. // Ext Function For CommonCollection <Pet> by Ray Linn
  14. Public static void FeedPet (this CommonCollection <Pet> collection)
  15. {
  16. Foreach (Pet pet in list)
  17. System. Console. WriteLin (pet. Eating ());
  18. }
  19. }



In this way, we have implemented the reconstruction, and the two collections have achieved the same purpose. After I restructured the Cecil, the size of the compiled Assemly is halved.


In C language ^ how to use a1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].
C Language & |! What is the? & operator used to extract the address of a variable.
For example, if you define a variable, the system will allocate a space in the memory during compilation.
The location of the space in the memory is its address. & Extract its address.
E. g int a; assign an address to it during compilation, for example, 2000; & a is 2000.
If an integer pointer Variable p, p = & a; is defined, the address 2000 of a is assigned to p. P = 2000 after running.
Another example is scanf ("% d", & a). When you enter 3, it first knows the address of a according to & a, and finds the space of a in the memory by the address, write 3 to this space.
* Is a pointer operator, which is opposite to &. It extracts the value of a Variable Based on the address of the variable.
For example, * the value of a is 3 of variable.
The following is a summary of the pointer used in the definition and description.
Int * p; defines a pointer to integer data.
Int * p [n]; defines the pointer array p, which consists of n pointer elements pointing to integer data.
Int (* p) [n]; p is the pointer variable pointing to a one-dimensional array containing n elements.
Int * p (); p is the function that returns a pointer pointing to integer data.
Int (* p) (); p is the pointer to the function. This function returns an integer value.
Int ** p; p is a pointer variable that points to an integer Data Pointer variable.
If you want to learn more about the system, you can refer to tan haoqiang's c Programming (the third edition), which is easy to understand. Is a good C language learning material.

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.