Search code examples
c#.netinterfaceboxingexplicit-interface

Why does calling an explicit interface implementation on a value type cause it to be boxed?


My question is somewhat related to this one: How does a generic constraint prevent boxing of a value type with an implicitly implemented interface?, but different because it shouldn't need a constraint to do this because it's not generic at all.

I have the code

interface I { void F(); }
struct C : I { void I.F() {} }
static class P {
    static void Main()
    {    
        C x;
        ((I)x).F();
    }
}

The main method compiles to this:

IL_0000:  ldloc.0
IL_0001:  box        C
IL_0006:  callvirt   instance void I::F()
IL_000b:  ret

Why doesn't it compile to this?

IL_0000:  ldloca.s   V_0
IL_0002:  call       instance void C::I.F()
IL_0007:  ret

I see why you need a method table to make a virtual call, but you don't need to make a virtual call in this case. If the interface is implemented normally it doesn't make a virtual call.

Also related: Why are explicit interface implementations private? - the existing answers on this question don't adequately explain why the methods are marked as private in the metadata (rather than merely having unusable names). But even this doesn't fully explain why it's boxed, since it still boxes when called from inside C.


Solution

  • I think the answer is in the C# specification of how interfaces can be treated. From the Spec:

    There are several kinds of variables in C#, including fields, array elements, local variables, and parameters. Variables represent storage locations, and every variable has a type that determines what values can be stored in the variable, as shown by the following table.

    Under the table that follows it says for an Interface

    A null reference, a reference to an instance of a class type that implements that interface type, or a reference to a boxed value of a value type that implements that interface type

    It says explicitly that it will be a boxed value of a value type. The compiler is just obeying the specification

    ** Edit **

    To add more information based upon the comment. The compiler is free to rewrite if it has the same effect but because the boxing occurs you make a copy of the value type not have the same value type. From the specification again:

    A boxing conversion implies making a copy of the value being boxed. This is different from a conversion of a reference-type to type object, in which the value continues to reference the same instance and simply is regarded as the less derived type object.

    This means it has to do the boxing every time or you'd get inconsistent behavior. A simple example of this can be shown by doing the following with the provided program:

    public interface I { void F(); }
    public struct C : I {
        public int i;
        public void F() { i++; } 
        public int GetI() { return i; }
    }
    
        class P
        {
        static void Main(string[] args)
        {
            C x = new C();
            I ix = (I)x;
            ix.F();
            ix.F();
            x.F();
            ((I)x).F();
            Console.WriteLine(x.GetI());
            Console.WriteLine(((C)ix).GetI());
            Console.ReadLine();
        }
    }
    

    I added an internal member to struct C that is incremented by 1 every time that F() is called on that object. This lets us see what is happening to the data of our value type. If boxing was not performed on x then you would expect the program to write out 4 for both calls to GetI() as we call F() four times. However the actual result we get is 1 and 2. The reason is that the boxing has made a copy.

    This shows us that there is a difference between if we box the value and if we don't box the value