[C #] Experience what is called a strong type

Source: Internet
Author: User
Tags tostring
First of all, this article is only a personal learning experience, not standard data. If there is any fallacy, welcome the expert to point out.

Object O1 = new Object ();
string S1 = O1.    ToString (); S1 would be "System.Object"

Object O2 = "Hello";
String s2 = O2.    ToString (); S2 would be "Hello"

This is very different from C + +. In C + +, O2. ToString () calls the object's ToString () method instead of string's ToString () method. O2 is a reference to a static string object "Hello", O2, although syntactically written as an object type, represents a string object in memory. When the program attempts to invoke the ToString () method via O2, the system first finds the object represented by the O2 in memory, and then finds the appropriate method to execute in the function table of the object. O2 represents a String object, and the string type has overloaded the ToString () method, and the ToString () method is immediately found in the function table of the string object represented by O2, so the string ToString () The method is invoked.
This is the strong type based on the runtime.

Specific to the difference in implementation:
C + + is compiled into a compilation, the call is JMP [address in memory], direct jump.
C # is compiled into IL, and calls are callvirt classname.functionname and need to be translated by runtime. Although the end is to JMP to an address to execute, but the jump to this address, but by the runtime in the runtime according to Classname.functionname and classinstance addresses calculated.

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.