.Net 如何理解嵌套的值类型?它们是值类型吗?
How does .Net understand nested valuetypes? Are they valuetypes?
在这种情况下:
public struct SectorLocator
{
public Surface Side { get; init; } //this is an enum-int
public VerticalPortion Section { get; init; } //this is another enum-int
}
public struct DataLocator
{
public SectorLocator Sector{get; init;}
public MeasureType Measure { get; init; } //this is another enum-int;
}
DataLocator 仍然是值类型吗?或者就像将引用类型放入结构中一样?
当您将扇区 属性 作为参数传递时它的行为如何?
我没有找到足够清楚的答案。
是的,DataLocator
仍然是一个值类型;完全由 struct
项定义。值类型可以 包含 引用,但这不会改变它的行为方式,除了:你不能在 unmanaged
约束中使用它,如果它确实包含引用(并且某些 API,例如 Unsafe
/MemoryMarshal
等可能会拒绝与您一起玩)。这实际上与询问 System.Runtime.CompilerServices.RuntimeHelpers.IsReferenceOrContainsReferences<T>()
相同:对于 不 包含引用的值类型,这将 return false
,并且 true
用于任何其他情况。
在这种情况下:
public struct SectorLocator
{
public Surface Side { get; init; } //this is an enum-int
public VerticalPortion Section { get; init; } //this is another enum-int
}
public struct DataLocator
{
public SectorLocator Sector{get; init;}
public MeasureType Measure { get; init; } //this is another enum-int;
}
DataLocator 仍然是值类型吗?或者就像将引用类型放入结构中一样? 当您将扇区 属性 作为参数传递时它的行为如何?
我没有找到足够清楚的答案。
是的,DataLocator
仍然是一个值类型;完全由 struct
项定义。值类型可以 包含 引用,但这不会改变它的行为方式,除了:你不能在 unmanaged
约束中使用它,如果它确实包含引用(并且某些 API,例如 Unsafe
/MemoryMarshal
等可能会拒绝与您一起玩)。这实际上与询问 System.Runtime.CompilerServices.RuntimeHelpers.IsReferenceOrContainsReferences<T>()
相同:对于 不 包含引用的值类型,这将 return false
,并且 true
用于任何其他情况。