在 C# 中使用整数枚举而不进行强制转换

发布于 2024-10-03 09:57:38 字数 685 浏览 4 评论 0原文

假设我有枚举,

public enum Test : int 
{ 
   TestValue1 = 0, 
   TestValue2, 
   TestValue3 
}

为什么我不能使用像 Int32 IntTest = Test.TestValue1 这样的语句而不进行强制转换来表示 IntTest = 0?如果我稍后决定向枚举添加更多项目,这会有用吗?我想我被迫使用 Int32 IntTest = (Int32) Test.TestValue1 ,我认为这应该是多余的......另外,为什么我不能做出类似的东西

switch (IntTest)
{
    case (Test.TestValue1) : DoSomething();
                             break;
    case (Test.TestValue2) : DoSomethingElse();
                             break;
    default                : Do Nothing();
}

The compiler said it Expects a Constant value 代替 TestValue1...该值不是常量吗?

Let's say I have the enum

public enum Test : int 
{ 
   TestValue1 = 0, 
   TestValue2, 
   TestValue3 
}

Why can't I use statements like Int32 IntTest = Test.TestValue1 without a cast to mean IntTest = 0? It would be useful if I decided later to add more items to the enumeration? I think I am forced to use Int32 IntTest = (Int32) Test.TestValue1, which I think should be redundant... Also, why can't I make something like

switch (IntTest)
{
    case (Test.TestValue1) : DoSomething();
                             break;
    case (Test.TestValue2) : DoSomethingElse();
                             break;
    default                : Do Nothing();
}

The compiler says it expects a constant value in the place of TestValue1... Isn't that value aready constant?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

水水月牙 2024-10-10 09:57:38

请注意,您可以这样做:

switch ((Test)IntTest)
{
    case (Test.TestValue1) : DoSomething();
                             break;
    case (Test.TestValue2) : DoSomethingElse();
                             break;
    default                : DoNothing();
}

即使该值不存在于枚举中,从 int 到 Test 的转换也保证不会失败。如果枚举中不存在该值,则在 Test 实例上调用 ToString() 将返回基础数值的字符串表示形式。

Note that you can do this:

switch ((Test)IntTest)
{
    case (Test.TestValue1) : DoSomething();
                             break;
    case (Test.TestValue2) : DoSomethingElse();
                             break;
    default                : DoNothing();
}

The cast to Test from int is guaranteed not to fail, even if the value is not present in the enumeration. If the value is not present in the enumeration, calling ToString() on the Test instance will return the string representation of the underlying numeric value.

溺深海 2024-10-10 09:57:38

您不能互换使用 Int32Test,因为从语义上讲,它们是不同的类型,即使它们碰巧使用相同的底层存储机制。

如果您需要 Int32,请使用 Int32;如果您需要测试,则使用测试;如果您需要在它们之间进行转换,请使用显式强制转换。

(当然,该语言可以很容易地被指定为允许隐式转换,但这样的“功能”可能会产生比它解决的问题更多的问题。)

You can't use Int32 and Test interchangeably because semantically they're different types, even if they do happen to use the same underlying storage mechanism.

If you need an Int32 then use an Int32; if you need a Test then use a Test; if you need to convert between them then use an explicit cast.

(And, of course, the language could easily have been spec'd to allow these conversions implicitly, but such a "feature" would probably create more problems than it solved.)

梦旅人picnic 2024-10-10 09:57:38

我想以与此相同的方式实现枚举,以便更轻松地将使用表列名称作为字符串的应用程序转换为索引。

这就是我想要实现的目标:

enum TableName
{
    Column1Name = 0
}

Column1Data = (cast)objData[RowIndex][TableName.Column1Name];

上面提到的麻烦是 C# 在使用枚举时需要显式强制转换

Column1Data = (cast)objData[RowIndex][(int)TableName.Column1Name]; //what a waste of time..

但我有一个解决方法.. 抱怨你想要的一切 :P (这不是生产应用程序 - 请思考在实际环境中实现此代码之前,请仔细考虑该代码的影响)

public static class TableName
{
    public const int Column1Name = 0;
}

Column1Data = (cast)objData[RowIndex][TableName.Column1Name]; //this is now valid, and my sanity remains intact - as does the integer value ;)

I wanted to implement an enum in the same way as this to simply make it easier to convert an application using table column names as strings to indexes.

This is what I'm trying to achieve:

enum TableName
{
    Column1Name = 0
}

Column1Data = (cast)objData[RowIndex][TableName.Column1Name];

the trouble as mentioned above is that C# requires an explicit cast when using enum's

Column1Data = (cast)objData[RowIndex][(int)TableName.Column1Name]; //what a waste of time..

But I've got a workaround.. moan all you want :P (this is not a production application - please think carefully about the impact of this code before implementing it in a live environment)

public static class TableName
{
    public const int Column1Name = 0;
}

Column1Data = (cast)objData[RowIndex][TableName.Column1Name]; //this is now valid, and my sanity remains intact - as does the integer value ;)
行雁书 2024-10-10 09:57:38

你必须强制转换,因为语言定义表明你必须强制转换。多余的?或许。但事实就是这样。

对于 switch 语句:

switch (IntTest)
{
    case (int)Test.TestValue1: DoSomething(); break;
}

枚举值必须使用枚举类型的名称进行完全限定。

You have to cast because the language definition says you have to cast. Redundant? Maybe. But that's the way it is.

For your switch statement:

switch (IntTest)
{
    case (int)Test.TestValue1: DoSomething(); break;
}

Enum values must be fully qualified with the name of the enum type.

っ〆星空下的拥抱 2024-10-10 09:57:38
public enum Test
{ 
   TestValue1 = 0, 
   TestValue2, 
   TestValue3 
}

在方法中,您可以使用枚举作为类型。
只需打开 Test 类型的变量,就不需要将 enum 强制转换为 int。

public void DoSwitch(Test val)
{
   switch (val)
   {
       case (Test.TestValue1) : DoSomething();
                                break;
       case (Test.TestValue2) : DoSomethingElse();
                                break;
       default                : Do Nothing();
   }
}
public enum Test
{ 
   TestValue1 = 0, 
   TestValue2, 
   TestValue3 
}

In a method you can use the enum as a Type.
Simply switch on a variable of type Test, then you do not need to cast the enum to an int.

public void DoSwitch(Test val)
{
   switch (val)
   {
       case (Test.TestValue1) : DoSomething();
                                break;
       case (Test.TestValue2) : DoSomethingElse();
                                break;
       default                : Do Nothing();
   }
}
眼藏柔 2024-10-10 09:57:38

枚举的设计就像这样
正如msdn所说:

基础类型指定多少
存储空间分配给每个
枚举器。然而,明确的强制转换
需要从枚举类型转换
为整数类型。

enums works like that by design
As msdn says :

The underlying type specifies how much
storage is allocated for each
enumerator. However, an explicit cast
is necessary to convert from enum type
to an integral type.

年华零落成诗 2024-10-10 09:57:38

嘿嘿,我刚读完书里的这一章。

我想提到的第一件事是你尝试这个

Console.WriteLine(Test.TestValue1);

你会注意到一些事情。

输出不是数字,甚至不是整数。

此外,这也应该回答你的第二个问题。

因此,您每次都必须显式地将枚举转换为整数。

:)

Hey hey, I have just read this chapter in my book.

First thing I would like to mention you is trying this

Console.WriteLine(Test.TestValue1);

You will notice something.

The output is not a number or even an integer.

Besides, this should answer your second question too.

So you have to explicitly cast your enum to integer every time.

: )

深白境迁sunset 2024-10-10 09:57:38

正如保罗指出的那样,您有一个语法错误,应该解决(您显然已修复该错误)。正如其他人所指出的,Test 是一个保存整数值的枚举。例如,可以使用枚举轻松处理 x0001 或 0xA0BC (41148) 等值。

在示例中,您使用了 Int32 IntTest,它是一个 32 位整数;我可能会补充说,您应该使用 int 而不是 Int32 ,除非您的枚举包含除整数之外的其他内容,甚至不指定 : int > 因为默认的持有类型确实是整数。

关于编译器强制您对值进行大小写的原因;它的工作原理是,枚举不是整数,只有其可能的子属性( TestValue1、TestValue2、TestValue3 )的值才是整数。*

正如有人指出的,Console.WriteLine 中将打印的内容不是整数。*

As Paul pointed out you have a syntax error that should resolve ( which you have apparently fixed ). As other(s) have pointed out Test is an enumeration which holds the values of an integer. An enumeration can be used for example to handle values like x0001 or 0xA0BC (41148) easily.

In the example you used Int32 IntTest which is a 32-bit integer; I might add you should be using int instead of Int32 and unless your enumeration is holding something other then an integer don't even specify the : int since the default holding type is indeed integer.

With regards to the reason the compiler forces you to case the value; Its just how it works, an enumeration is NOT an integer, only the values of its possible sub-properties( TestValue1, TestValue2, TestValue3 ) are integers.*

As somebody pointed out what will be printed in a Console.WriteLine is not an integer.*

笑看君怀她人 2024-10-10 09:57:38
  • 首先,枚举的 : int 部分是完全多余的,因为 int 已经是枚举的默认控制类型。

  • 需要强制转换,因为当您编写枚举时,您实际上是在定义一个新的类型,就像一个类一样。名为 Test 的枚举的任何值都是 Test 类型。

  • First, the : int part of your enum is completely redundant since int is the default governing type for enum already.

  • A cast is needed because when you write an enum, you are actually defining a new Type, much like a class. Any value of the enum named Test is of the type, Test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文