是枚举到枚举拳击还是只是类型转换

Is enum to Enum boxing or just type casting

我想知道转换为枚举是否会封装枚举

所以我应该写 expression bodied 成员来减小我的对象的大小。

public Fruit FruitType => (Fruit) Type; // unboxing?
public override Enum Type => (Fruit) (Data[0] & 0xF0); // boxing?

或者有属性防止装箱和拆箱?

public Fruit FruitType => (Fruit) (Data[0] & 0xF0);
public override Enum Type { get; } = (Fruit) (Data[0] & 0xF0); // assigned once.

假设我有数千个实例。这个属性被使用了大约6万次。

是的,值被装箱了。

the C# spec 的第 4.3.1 节 "Boxing conversions" 指出:

A boxing conversion permits a value-type to be implicitly converted to a reference-type. The following boxing conversions exist:

[...]

  • From any enum-type to the type System.Enum.

[...]