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.