Do interface variables have value-type or reference-type semantics?

后端 未结 4 627
醉酒成梦
醉酒成梦 2021-02-02 17:13

Do interface variables have value-type or reference-type semantics?

Interfaces are implemented by types, and those types are either value types or reference types. Obvi

4条回答
  •  太阳男子
    2021-02-02 17:47

    This is about understanding boxing and unboxing of types. In your example, the int is boxed upon assignment and a reference to that "box" or object is what is assigned to x. The value type int is defined as a struct which implements IComparable. However, once you use that interface reference to refer to the value type int, it will be boxed and placed on the heap. That's how it works in practice. The fact that using an interface reference causes boxing to occur by definition makes this reference type semantics.

    MSDN: Boxing and Unboxing

提交回复
热议问题