C# 中长值的枚举

发布于 2024-11-30 07:24:53 字数 473 浏览 0 评论 0原文

为什么这个声明

public enum ECountry : long
{
    None,
    Canada,
    UnitedStates
}

需要对其任何值进行强制转换?

long ID = ECountry.Canada;
// Error Cannot implicitly convert type 'ECountry' to 'long'.
// An explicit conversion exists (are you missing a cast?)

除了强制转换之外,还有其他方法可以直接从枚举中获取 long 值吗?

这也行不通,例如:

public enum ECountry : long
{
    None = 0L,
    Canada = 1L,
    UnitedStates=2L
}

Why does this declaration,

public enum ECountry : long
{
    None,
    Canada,
    UnitedStates
}

require a cast for any of its values?

long ID = ECountry.Canada;
// Error Cannot implicitly convert type 'ECountry' to 'long'.
// An explicit conversion exists (are you missing a cast?)

And is there a way to get a long value directly from the enum, besides casting?

This would not work either, for example:

public enum ECountry : long
{
    None = 0L,
    Canada = 1L,
    UnitedStates=2L
}

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

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

发布评论

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

评论(5

最好是你 2024-12-07 07:24:54

AFAIK,你必须铸造。

MSDN 文章说:

“但是,需要显式转换才能从枚举类型转换为整型”(int 或 long)

您可以查看:

枚举类型(C# 参考)

AFAIK, you have got to cast.

The MSDN article says:

"However, an explicit cast is needed to convert from enum type to an integral type" (either int or long)

You can check it out:

Enumeration types (C# reference)

热血少△年 2024-12-07 07:24:53

问题是底层类型仍然是int。它是 long,您可以为成员分配 long 值。但是,您永远不能只将枚举值分配给整型而不进行强制转换。这应该有效:

public enum ECountry : long
{
    None,
    Canada,
    UnitedStates = (long)int.MaxValue + 1;
}

// val will be equal to the *long* value int.MaxValue + 1
long val = (long)ECountry.UnitedStates;

The issue is not that the underlying type is still int. It's long, and you can assign long values to the members. However, you can never just assign an enum value to an integral type without a cast. This should work:

public enum ECountry : long
{
    None,
    Canada,
    UnitedStates = (long)int.MaxValue + 1;
}

// val will be equal to the *long* value int.MaxValue + 1
long val = (long)ECountry.UnitedStates;
金橙橙 2024-12-07 07:24:53

enum 的默认基础类型是 intenum 可以是除 char 之外的任何整型。

如果您希望它很长,您可以执行以下操作:

// Using long enumerators
using System;
public class EnumTest 
{
    enum Range :long {Max = 2147483648L, Min = 255L};
    static void Main() 
    {
        long x = (long)Range.Max;
        long y = (long)Range.Min;
        Console.WriteLine("Max = {0}", x);
        Console.WriteLine("Min = {0}", y);
    }
}

这里最重要的是强制转换。正如 @dlev 所说,在 enum 中使用 long 的目的是支持大量标志(超过 32 个,因为 2^32 是 4294967296,并且 >long 可以容纳超过 2^32)。

The default underlying type of enum is int. An enum can be any integral type except char.

If you want it to be long, you can do something like this:

// Using long enumerators
using System;
public class EnumTest 
{
    enum Range :long {Max = 2147483648L, Min = 255L};
    static void Main() 
    {
        long x = (long)Range.Max;
        long y = (long)Range.Min;
        Console.WriteLine("Max = {0}", x);
        Console.WriteLine("Min = {0}", y);
    }
}

The cast is what is important here. And as @dlev says, the purpose of using long in an enum is to support a large number of flags (more than 32 since 2^32 is 4294967296 and a long can hold more than 2^32).

旧时模样 2024-12-07 07:24:53

必须强制转换枚举以从中获取值,否则它将保留枚举类型。

You must cast an enum to get a value from it or it will remain an enum type.

<逆流佳人身旁 2024-12-07 07:24:53

MSDN:

来自 在本例中,基类型选项用于声明一个成员为 long 类型的枚举。请注意,即使枚举的基础类型是 long,枚举成员仍然必须使用强制转换显式转换为 long 类型。

// keyword_enum2.cs
// Using long enumerators
using System;
public class EnumTest
{
    enum Range :long {Max = 2147483648L, Min = 255L};
    static void Main()
    {
        long x = (long)Range.Max;
        long y = (long)Range.Min;
        Console.WriteLine("Max = {0}", x);
        Console.WriteLine("Min = {0}", y);
    }
}

输出

最大值 = 2147483648 最小值 = 255

From MSDN:

In this example, the base-type option is used to declare an enum whose members are of the type long. Notice that even though the underlying type of the enumeration is long, the enumeration members must still be explicitly converted to type long using a cast.

// keyword_enum2.cs
// Using long enumerators
using System;
public class EnumTest
{
    enum Range :long {Max = 2147483648L, Min = 255L};
    static void Main()
    {
        long x = (long)Range.Max;
        long y = (long)Range.Min;
        Console.WriteLine("Max = {0}", x);
        Console.WriteLine("Min = {0}", y);
    }
}

Output

Max = 2147483648 Min = 255

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