Does using(IDisposable) induce boxing?

If I have a value type Foo:


struct Foo : IDisposable
{
    void Dispose() {};
}

 


And I place an instance of Foo in a using block:


Foo bar = new Foo();
using(bar)
{
    ...
}

 


My understanding is that bar.Dispose() will be called at the end of the using block. My question is, does this induce boxing Since Foo is a value type, and IDisposable is not, will the C# compiler cast bar to type IDisposable before calling Dispose I imagine this would be a boxing operation. Or can it call foo.Dispose() without having to cast to IDisposable


Answer this question

Does using(IDisposable) induce boxing?

  • Fer Mayorga

    Boxing is explicit in IL, hence ILDASM has the answer: No boxing

      IL_0000:  ldloca.s   V_0
      IL_0002:  initobj    MainClass/Foo
      IL_0008:  ldloc.0
      IL_0009:  stloc.1
      .try
      {
        IL_000a:  leave.s    IL_0014
      }  // end .try
      finally
      {
        IL_000c:  ldloca.s   V_1
        IL_000e:  call       instance void MainClass/Foo::Dispose()
        IL_0013:  endfinally
      }  // end handler
      IL_0014:  ret

    Diego Gonzalez
    Lagash Systems SA
    C# MVP

  • Erik11

    Thanks, that answers it then.

  • Does using(IDisposable) induce boxing?