Whose ToString () is being called?

As you know, it inthas a method ToString()that overrides a ToString()base type method Object.

For this next code

int x = 100;
object y = (object)x;
Console.Write(y.ToString());

(1) The challenge ToString()? int or object? WHY?
(2) How can we verify / find out the truth? Using any debugging / tool?

+5
source share
4 answers

Since the value is squared, all the compiler knows is that this objectis why this is a regular virtual call object.ToString()that will then raise an overridden ToString()structure. Thus: it object.ToString() is called , and Int32.ToString()overrides which is executed .

private static void Main()
{
    int x = 100;
    object y = (object)x;
    Console.Write(y.ToString());
}

becomes (my comments):

.method private hidebysig static void Main() cil managed
{
    .entrypoint
    .maxstack 1
    .locals init (
        [0] int32 x,
        [1] object y)

    // int x = 100;
    L_0000: ldc.i4.s 100
    L_0002: stloc.0

    // object y = (object)x; 
    L_0003: ldloc.0 
    L_0004: box int32
    L_0009: stloc.1

    // Console.Write(y.ToString());
    L_000a: ldloc.1 
    L_000b: callvirt instance string [mscorlib]System.Object::ToString()
    L_0010: call void [mscorlib]System.Console::Write(string)
    L_0015: ret 
}

L_000b; object.ToString().

, -boxed value-types; , ToString(), :

private static void Main()
{
    int x = 100;
    Console.Write(x.ToString());
}

.method private hidebysig static void Main() cil managed
{
    .entrypoint
    .maxstack 1
    .locals init (
        [0] int32 x)
    L_0000: ldc.i4.s 100
    L_0002: stloc.0 
    L_0003: ldloca.s x
    L_0005: call instance string [mscorlib]System.Int32::ToString()
    L_000a: call void [mscorlib]System.Console::Write(string)
    L_000f: ret 
}

. (call) L_0005. HOWEVER, , JIT , , , :

private static void Main()
{
    var x = new KeyValuePair<int, string>(123, "abc");
    Console.Write(x.ToString());
}

:

.method private hidebysig static void Main() cil managed
{
    .entrypoint
    .maxstack 3
    .locals init (
        [0] valuetype [mscorlib]System.Collections.Generic.KeyValuePair`2<int32, string> x)
    L_0000: ldloca.s x
    L_0002: ldc.i4.s 0x7b
    L_0004: ldstr "abc"
    L_0009: call instance void [mscorlib]System.Collections.Generic.KeyValuePair`2<int32, string>::.ctor(!0, !1)
    L_000e: ldloca.s x
    L_0010: constrained [mscorlib]System.Collections.Generic.KeyValuePair`2<int32, string>
    L_0016: callvirt instance string [mscorlib]System.Object::ToString()
    L_001b: call void [mscorlib]System.Console::Write(string)
    L_0020: ret 
}

"" / "callvirt" L_0010 L_0016 , . JIT/runtime . .

, class , return base.ToString();, .

+3

Int32.ToString() , ToString() . Int32.ToString() Object.ToString(), Object.ToString().
y int.

+4

Int32, . , . , "System.Object".

+2

, java. !:) int - . , , . , ! ( toString, )

, toString. Integer, int, ( ), toString (, ) toString (, Integer class), toString.

toString , . didnt nobody (: Object), toString.

:

Integer x = new Integer(3);

Print

x.toString()
((Object) x).toString();

And a new anonymous class, same as Integer, but toString Overriden

Integer y = new Integer(3){
    @Override
    String toString(){
    return "WHATEVER!!";

    }
    };

Print

y.toString();
0
source

All Articles