[C #] Strong Type

xiaoxiao2021-03-06  43

First, statement, this article is just a personal learning experience, not standard information. If there is any falcation, welcome the master pointing 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 () will call the Object's toString () method, not a string toString () method. O2 is a reference to a static String object "Hello", and O2 is written in the grammatical object, but it represents a String object in memory. When the program tries to call the toString () method by O2, the system first wants to find the object represented by O2 in the memory, and then find the appropriate method in this object table. O2 represents a String object, and the String type has been overloaded to the toString () method, and the toString () method is found immediately in the function table of the String object represented by O2, so the TSTRING () method of the String type is Call.

This is based on the runtime.

Distinguished distinction:

C is compiled into compilation, call is JMP [address in memory], directly jump.

C # is compiled into IL, calling is callvirt classname.functionName, which requires Runtime translation. Although it is ultimately to be executed to an address, the address of the jump is calculated based on ClassName.FunctionName and ClassInstance Address when Runtime is running.

转载请注明原文地址:https://www.9cbs.com/read-74357.html

New Post(0)