[C #] strong type

Source: Internet
Author: User
First, declare that this article ArticleIt is just an individual's learning experience, not a standard document. If there are any mistakes, you are welcome to point out.

Object O1 = new object ();
String S1 = o1.tostring (); // S1 will be "system. Object"

Object O2 = "hello ";
String S2 = o2.tostring (); // S2 will be "hello"

This is very different from C ++. In C ++, o2.tostring () calls the tostring () method of the object instead of the tostring () method of the string. O2 is a reference to the static String object "hello". Although O2 is written as the object type in syntax, it represents a String object in memory. WhenProgramWhen trying to call the tostring () method through O2, the system first needs to find the object represented by O2 in the memory, and then find a proper method in the function table of this object for execution. O2 represents a String object, while the string type has been overloaded with the tostring () method. The tostring () method is immediately found in the function table of the string object represented by O2, therefore, the tostring () method of the string type is called.
This is based on the strong type of runtime.

Differences in implementation:
C ++ is compiled into an Assembly and called as JMP [Address in memory.
C # is compiled into Il. The call is callvirt classname. functionname, which must be translated by runtime. Although JMP is executed to a specific address, the jump address is calculated by runtime according to classname. functionname and classinstance address during runtime.

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.