C #(9 consistent)

Source: Internet
Author: User

9. Consistent types

Most languages have basic types (int, long, and so on ). Advanced types are ultimately composed of basic types. It is usually useful to process basic and advanced types in the same way. For example, it is useful if the set can be as inclusive as sting. To this end, Smalltalk sacrifices some efficiency to process int and long as it processes string or Form. Java tries to avoid this efficiency loss. It processes basic types like C and C ++, but provides corresponding packaging classes for each basic type-int packaged as Integer, double is packaged as Double. The C ++ template parameters can be of any type, as long as the type provides the implementation of the template-defined operations.

In Java, you can write:

Int I = 1;

Double d = 1.1;

Integer iObj = new Integer (1 );

Double dObj = new Double (1.1 );

The following statements are incorrect:

Int I = new int (1 );

Integer iObj = 1;

]

C # provides a different solution to this problem. In the previous section, I introduced the structure in C # and pointed out that the basic type is just an alias of the structure. Since the structure has methods of all object types, the code can be written as follows:

Int I = 5;

System. Console. WriteLine (I. ToString ());

If we want to use a structure like an object, C # will pack the structure as an object for you. When you need to use the structure again, you can implement it by unpacking:

Stack stack = new Stack ();

Stack. Push (I); // boxed

Int j = (int) stack. Pop ();File: // splitBox

Unpacking is not only required for type conversion, but also a seamless way to handle the relationship between structures and classes. You need to understand that packing is done to create packaging classes, although CLR can provide additional Optimization for boxed objects.

In C #, the following packaging classes exist for any value (structure) type:

Class T_BoxFile: // TRepresents any value type

{

T Value;

T_Box (T t) {Value = t ;}

}

When packing, for example:

Int n = 1;

Object box = n;

It is equivalent:

Int n = 1;

Object box = new int_Box (I );

When unpacking, for example:

Object box = 1;

Int n = (int) box;

It is equivalent:

Object box = new int_Box (1 );

Int n = (int_Box) box). Value ;]

[Note: The designers of C # should consider the template during the design process. I suspect there are two reasons for not using templates: the first is chaos. Templates may be difficult to integrate with object-oriented features, which brings too much (CHAOS) to programmers) design possibilities, and it is difficult to work with reflection; the second point is, if.. NET Library (such as collection class) does not use a template, the template will not be very useful. However, if the. NET class uses them, more than 20 languages using the. NET class will have to work with the template, which is technically difficult to implement.

It is interesting to note that the template (generic) has been considered by the Java Community to be incorporated into the Java language specification. Maybe every company will sing a different tone-Sun says ". NET has a minimum denominator syndrome", while Microsoft says "Java does not support multiple languages ".

(Apologize on March 13, August 10) After reading an interview with Anders Hejlsberg (Http://windows.oreilly.com/news/hejlsberg_0800.html), It seems that the template has come to the horizon, but the first version does not, because of the difficulties we mentioned above. We can see that the IL specification is written so that the IL code can present the template (using a non-destructive method to make reflection work well) bytecode cannot be a very interesting thing. Here, I also provide a link for the Java Community to consider adding generics:Http://jcp.org/jsr/detail/014.jsp]

[Note: Here is the Chinese version of the interview with Anders Hejlsberg mentioned above:Http://www.csdn.net/develop/article/11/11580.shtm. For more information, see

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.