I have the following simple C# code:

private Stack<Person> m_stack = new Stack<Person>();

public void Add<T>(T obj)
  where T : Person
{
     m_stack.Push(obj);
}

This will produce the following IL code:

  .method public hidebysig instance void 
          Add<(ConsoleApplication1.Person) T>(!!T obj) cil managed
  {
    // Code size       20 (0x14)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ldarg.0
    IL_0002:  ldfld      class [System]System.Collections.Generic.Stack`1<class ConsoleApplication1.Person> ConsoleApplication1.Pool::m_stack
    IL_0007:  ldarg.1
    IL_0008:  box        !!T
    IL_000d:  callvirt   instance void class [System]System.Collections.Generic.Stack`1<class ConsoleApplication1.Person>::Push(!0)
    IL_0012:  nop
    IL_0013:  ret
  } // end of method Pool::Add

So my question is... why boxing? (IL_0008) I can understand downcasting or even compile error, but why boxing (Person is a reference type...)

Thanks in advance!

Edit
Report