通过铸造界面拳击
boxing through casting interfaces
如果我有一个接口A并且继承了class B:
interface A { }
class B : A { }
并写下:
B sth = new B();
A aaa = B;
B bbb = (B)aaa;
会不会出现拳击?
当然A和B也不为空
装箱只出现在值类型中。 B
是引用类型,所以没有,没有装箱。
如果 B
是一个值类型(struct
而不是 class
),那么是的,装箱会发生。
interface IA { }
struct B : IA { }
B b = new B();
IA a = b; //boxing, converting a value type into a reference type
b = (B)a; //unboxing, converting a boxed value type back to the value type itself
在处理结构和接口时避免这些装箱和拆箱操作的一种方法是定义一个通用接口,如 IEquatable<T>
、IComparable<T>
等,它使您能够 使用接口工作,而不实际装箱值类型本身。
没有。
Boxing is the process of converting a value type to the type object or
to any interface type implemented by this value type.
Here 更多。
如果我有一个接口A并且继承了class B:
interface A { }
class B : A { }
并写下:
B sth = new B();
A aaa = B;
B bbb = (B)aaa;
会不会出现拳击? 当然A和B也不为空
装箱只出现在值类型中。 B
是引用类型,所以没有,没有装箱。
如果 B
是一个值类型(struct
而不是 class
),那么是的,装箱会发生。
interface IA { }
struct B : IA { }
B b = new B();
IA a = b; //boxing, converting a value type into a reference type
b = (B)a; //unboxing, converting a boxed value type back to the value type itself
在处理结构和接口时避免这些装箱和拆箱操作的一种方法是定义一个通用接口,如 IEquatable<T>
、IComparable<T>
等,它使您能够 使用接口工作,而不实际装箱值类型本身。
没有。
Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type.
Here 更多。