int 是枚举的支持类型
How int is the backing type for enum
根据 this post int
是 enum
的支持类型。
当我检查 .NET 的源代码时 System.Enum abstract class inherits from System.ValueType 摘要 class.
但是当我检查 System.Int32 struct it inherits from interfaces but not from System.ValueType.
相反,当我反编译 mscorlib.dll 并检查 Int32
结构时,它说该结构的基类型为 System.ValueType
.
但是仍然检查反编译的源代码我看不到任何关于System.ValueType
的信息。
这让我觉得 struct
关键字声明了 auto-Sytem.ValueType,Microsoft 也在这个 reference.
中表示
但我还有一个问题。据我所知,两个不同的 classes 从同一父继承并不意味着一个也从另一个继承。我的意思是如果 B:A
和 C:A
这并不总是意味着 C:B
.
此外,当我检查源代码时,System.Enum
与 System.Int32
的实现有很大不同。
那么,在这种情况下,这与 'System.Int32' 作为 System.Enum
的支持类型有什么关系?
谁能解释一下?
int
和 enum
是两个截然不同的东西,例如,您可以用整数计算 (add/subtract),而枚举不能这样做。所以很明显,两者的实现是非常不同的。
枚举仍然存储与整数(通常是 32 位内存位置)相同的方式并且在枚举值和 int 值之间存在 1:1 转换.
在这个意义上,int 是枚举的支持类型。
Int32 只是枚举的默认基础类型,但是这是可以更改的。您可以通过指定不同的数据类型来更改它,例如
enum ExampleEnum : byte
{
Value1,
Value2,
Value3
};
Microsoft 关于枚举的说法如下:
Every enumeration type has an underlying type, which can be any integral type except char.
The default underlying type of enumeration elements is int.
To declare an enum of another integral type, such as byte, use a colon after the identifier followed by the type,
...
The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.
您混淆了 enum
(用于定义枚举类型的 C# 关键字)和 Enum
(此类枚举类型派生的 class)。
using System;
using System.Reflection;
enum Foo { A, B, C };
static class Program {
static void Main() {
foreach (var field in typeof(Foo).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
Console.WriteLine("Found instance field \"" + field.Name + "\" of type \"" + field.FieldType.FullName + "\"");
}
}
在我的系统上,这会打印
Found instance field "value__" of type "System.Int32"
这是因为 Foo
被有效地定义为(伪代码,无效的 C#)
struct Foo : Enum {
int value__;
}
除了一些额外的编译器支持,还有一些额外的静态字段来列出枚举的成员,但基本思想保持不变。
支持类型被定义为每个具体枚举类型的一部分,而不是 System.Enum
的一部分。不可能,因为两种不同的枚举类型可能不同。
由于 继承 和 表示 之间的差异,您会感到困惑。
仅仅因为 B
继承自 A
并不意味着 B
由 A
表示(支持)。
Enum
可以继承自 ValueType
,但由 int
表示(支持)。
有点像 Person
class 可以从 Object
继承,它使用 string
(姓名)和 int
(年龄)作为class.
的支持类型
最重要的是,编译器在处理多种类型时会发挥一些作用。 ValueType
继承自 Object
但它不是引用类型,因为编译器会专门处理它。
Enum
是一种特殊的 ValueType
,它具有自动创建的支持类型 - 默认情况下 int
- 这完全取决于编译器。
根据 this post int
是 enum
的支持类型。
当我检查 .NET 的源代码时 System.Enum abstract class inherits from System.ValueType 摘要 class.
但是当我检查 System.Int32 struct it inherits from interfaces but not from System.ValueType.
相反,当我反编译 mscorlib.dll 并检查 Int32
结构时,它说该结构的基类型为 System.ValueType
.
但是仍然检查反编译的源代码我看不到任何关于System.ValueType
的信息。
这让我觉得 struct
关键字声明了 auto-Sytem.ValueType,Microsoft 也在这个 reference.
但我还有一个问题。据我所知,两个不同的 classes 从同一父继承并不意味着一个也从另一个继承。我的意思是如果 B:A
和 C:A
这并不总是意味着 C:B
.
此外,当我检查源代码时,System.Enum
与 System.Int32
的实现有很大不同。
那么,在这种情况下,这与 'System.Int32' 作为 System.Enum
的支持类型有什么关系?
谁能解释一下?
int
和 enum
是两个截然不同的东西,例如,您可以用整数计算 (add/subtract),而枚举不能这样做。所以很明显,两者的实现是非常不同的。
枚举仍然存储与整数(通常是 32 位内存位置)相同的方式并且在枚举值和 int 值之间存在 1:1 转换.
在这个意义上,int 是枚举的支持类型。
Int32 只是枚举的默认基础类型,但是这是可以更改的。您可以通过指定不同的数据类型来更改它,例如
enum ExampleEnum : byte
{
Value1,
Value2,
Value3
};
Microsoft 关于枚举的说法如下:
Every enumeration type has an underlying type, which can be any integral type except char.
The default underlying type of enumeration elements is int.
To declare an enum of another integral type, such as byte, use a colon after the identifier followed by the type,
...
The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.
您混淆了 enum
(用于定义枚举类型的 C# 关键字)和 Enum
(此类枚举类型派生的 class)。
using System;
using System.Reflection;
enum Foo { A, B, C };
static class Program {
static void Main() {
foreach (var field in typeof(Foo).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
Console.WriteLine("Found instance field \"" + field.Name + "\" of type \"" + field.FieldType.FullName + "\"");
}
}
在我的系统上,这会打印
Found instance field "value__" of type "System.Int32"
这是因为 Foo
被有效地定义为(伪代码,无效的 C#)
struct Foo : Enum {
int value__;
}
除了一些额外的编译器支持,还有一些额外的静态字段来列出枚举的成员,但基本思想保持不变。
支持类型被定义为每个具体枚举类型的一部分,而不是 System.Enum
的一部分。不可能,因为两种不同的枚举类型可能不同。
由于 继承 和 表示 之间的差异,您会感到困惑。
仅仅因为 B
继承自 A
并不意味着 B
由 A
表示(支持)。
Enum
可以继承自 ValueType
,但由 int
表示(支持)。
有点像 Person
class 可以从 Object
继承,它使用 string
(姓名)和 int
(年龄)作为class.
最重要的是,编译器在处理多种类型时会发挥一些作用。 ValueType
继承自 Object
但它不是引用类型,因为编译器会专门处理它。
Enum
是一种特殊的 ValueType
,它具有自动创建的支持类型 - 默认情况下 int
- 这完全取决于编译器。