在 C# 中将 0 作为 enum 的默认值 。 如果我声明以不同数字开头的点数 : @ info: whatsthis
enum Color
{
Blue = 1,
Green,
Red,
}
然后 var color = default (Color)
- 将返回我 0。 我期待看到最小值, 而不是最小值。 如果每个查点成员对应某个特定数字, 0 不是一个有效的值, 这种行为可能会引起问题 。
在 C# 中将 0 作为 enum 的默认值 。 如果我声明以不同数字开头的点数 : @ info: whatsthis
enum Color
{
Blue = 1,
Green,
Red,
}
然后 var color = default (Color)
- 将返回我 0。 我期待看到最小值, 而不是最小值。 如果每个查点成员对应某个特定数字, 0 不是一个有效的值, 这种行为可能会引起问题 。
我们只能对 为什么 框架的一个方面设计了某种方式。 最直接的解释是, 我想从< a href=" http://msdn. microsoft. com/ en- us/library/1zt1ybx4" rel=“ noreferrer” > MSDN文档 中强调这句话:
An enumeration is a set of named constants whose underlying type is any integral type except Char. If no underlying type is explicitly declared, Int32 is used.
请注意,.NET 查点基本上是一个整体型号的延伸。 集成型号的默认值为 0, 所以查点以继承该行为是合理的( 在您所描述的案例中, 如果有些不方便的话 ) 。
所有值类型( 包括 enum) 的默认值为位元 0。 因此它意味着即使未明确定义 0 值, 但对于 enum 来说, 0 总是可能的值 。
说明如下:enum
enum 是特殊的类型, 其排序来自其他值类型( 通常不可能), 但它作为支持类型中的值类型行事。 enum 只能使用某些整体值作为支持类型 。
注意 : @ archil 指出 enum 值可能包含任何支持类型值, 不论在 enum 本身 中列出的常数 。 对于标记为“ lagsAttritte ” 的enum 来说, 这种行为通常会发生, 但对于普通的 enum 来说, 它可能不是直观的 。
eum总是有可能不存在价值。这就是为什么存在
正如迄今为止在多个解答中所指出的,任何固定尺寸数据结构的“默认”值为 0。结构内存的大小被设置为零,这被认为是“默认 ” 。现在,如果您想要定义一个 enum 的“ 默认” 值,您就有了多个选项。最容易的选项是写入您自己的函数, 给您一个“ 最小有效 enum ” 。 用法 : 您显然可以改变您如何确定“ 默认” 值, 但这种方式似乎最适合您的需求 。 为何默认 enum 值为 0? 运行时间执行的容易程度 。 任何值类型的默认值为 位元 0, 假设一个无效引用由 位元 0 表示 。 因此, 要初始构造一个带有默认值的对象, 我们只需要知道它的大小; 运行时间不需要知道或处理关于对象结构的任何内容 。 同样, 预定义的值类型构建器只能以零填充内存范围 。 不管它是否喜欢, 都是这样。 我们需要做的是当设计 enum 时, 无论名称是合适的默认值, 都给它值 0。 如果没有合适的默认值, 那么您接受 enum 作为参数的方法可以检查它是否设置得当, 例如 。 点是 probabbly 的值为最小正整数值 。 为什么偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏偏 能够正确使用位移操作来治疗 enum 。 我自然而然地在谈论默认值, 开发者可以改变它。 我不确定为什么是他们,但是... 这是关于C#enum的文献:http://msdn.microsoft.com/en-us/library/sbbt4032.aspx 如下示例所示,计算器可以使用初始化器取代默认值。 {Sat=1,日,月,月,月,月,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日,日; 在此罗列中, 元素序列必须从 1 开始而不是 0 。 但是, 推荐了一个值为 0 的常数 。 详情请见 < a href=" http:// msdn. microsoft. com/ en- us/library/cc138362. aspx" rel = "nofollow" > 见 编号类型( C# 编程指南) a > 。 他们建议您总是包含一个与值 0 相对应的占位符 enum 值, 否则您可能会遇到问题 。 您可以给这个 enum 值命名类似 : < code> Undered code > 或 < code> Default code > 或类似的东西 。 正如我在对您问题的评论中所提到的那样,我的再次建议是,除非您有很好的理由,否则不要将数值用于您的 enums 此代码 : 在IL中是这样的: 因此,在某些情况下,
What is the use of default keyword in C#?
Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs.
...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits.
Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class.
My import looks like this:
[DllImport(@"test.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber.
I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control.
...
private E GetDefaultEnum<E>()
{
return (E)(Enum.GetValues(typeof(E)).Cast<E>().Min<E>());
}
enum Color
{
Blue = 1,
Green,
Red,
}
Color defaultColor = GetDefaultEnum<Color>();
MessageBox.Show(defaultColor.ToString()); // "Blue"
void ProcessColor(Color c) {
if (c == 0)
throw new ArgumentException("Color is not set", "c");
//...
}
enum
is unt
> (默认情况下)。 Enums是源代码中人类可读的一亿字节,但在编译您的程序时,没有 enum
,只有 int
。 Int的默认值为 0。public enum test { One, Two, Three }
var x = test.One;
var y = 0;
IL_0001: ldc.i4.0
IL_0002: stloc.0
IL_0003: ldc.i4.0
IL_0004: stloc.1
int
怎么会有一个默认值为1(如你所要求),而其他的则没有?