C# 枚举可以声明为 bool 类型吗?

发布于 2024-08-15 03:35:10 字数 140 浏览 5 评论 0原文

我可以将 c# enum 声明为 bool 吗:

enum Result : bool
{
    pass = true,
    fail = false
}

Can I declare c# enum as bool like:

enum Result : bool
{
    pass = true,
    fail = false
}

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

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

发布评论

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

评论(3

酷到爆炸 2024-08-22 03:35:10

它表示

枚举的批准类型为byte、sbyte、short、ushort、int、uint、long 或 ulong

枚举(C# 参考)

It says

The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.

enum (C# Reference)

旧竹 2024-08-22 03:35:10

如果除了枚举常量的类型值之外,您还需要枚举包含布尔数据,则可以向枚举添加一个简单的属性,并采用布尔值。然后,您可以为枚举添加一个扩展方法,用于获取属性并返回其布尔值。

public class MyBoolAttribute: Attribute
{
        public MyBoolAttribute(bool val)
        {
            Passed = val;
        }

        public bool Passed
        {
            get;
            set;
        }
}

public enum MyEnum
{
        [MyBoolAttribute(true)]
        Passed,
        [MyBoolAttribute(false)]
        Failed,
        [MyBoolAttribute(true)]
        PassedUnderCertainCondition,

        ... and other enum values

}

/* the extension method */    
public static bool DidPass(this Enum en)
{
       MyBoolAttribute attrib = GetAttribute<MyBoolAttribute>(en);
       return attrib.Passed;
}

/* general helper method to get attributes of enums */
public static T GetAttribute<T>(Enum en) where T : Attribute
{
       Type type = en.GetType();
       MemberInfo[] memInfo = type.GetMember(en.ToString());
       if (memInfo != null && memInfo.Length > 0)
       {
             object[] attrs = memInfo[0].GetCustomAttributes(typeof(T),
             false);

             if (attrs != null && attrs.Length > 0)
                return ((T)attrs[0]);

       }
       return null;
}

if you need your enum to contain boolean data in addition to the enum constant's type value, you could add a simple attribute to your enum, taking a boolean value. Then you can add an extension method for your enum that fetches the attribute and returns its boolean value.

public class MyBoolAttribute: Attribute
{
        public MyBoolAttribute(bool val)
        {
            Passed = val;
        }

        public bool Passed
        {
            get;
            set;
        }
}

public enum MyEnum
{
        [MyBoolAttribute(true)]
        Passed,
        [MyBoolAttribute(false)]
        Failed,
        [MyBoolAttribute(true)]
        PassedUnderCertainCondition,

        ... and other enum values

}

/* the extension method */    
public static bool DidPass(this Enum en)
{
       MyBoolAttribute attrib = GetAttribute<MyBoolAttribute>(en);
       return attrib.Passed;
}

/* general helper method to get attributes of enums */
public static T GetAttribute<T>(Enum en) where T : Attribute
{
       Type type = en.GetType();
       MemberInfo[] memInfo = type.GetMember(en.ToString());
       if (memInfo != null && memInfo.Length > 0)
       {
             object[] attrs = memInfo[0].GetCustomAttributes(typeof(T),
             false);

             if (attrs != null && attrs.Length > 0)
                return ((T)attrs[0]);

       }
       return null;
}
计㈡愣 2024-08-22 03:35:10

怎么样:

class Result
    {
        private Result()
        {
        }
        public static Result OK = new Result();
        public static Result Error = new Result();
        public static implicit operator bool(Result result)
        {
            return result == OK;
        }
        public static implicit operator Result( bool b)
        {
            return b ? OK : Error;
        }
    }

例如,您可以像 Enum 或 bool 一样使用它
var x = 结果.OK;
结果 y = true;
如果(x)...
或者
if(y==结果.OK)

What about:

class Result
    {
        private Result()
        {
        }
        public static Result OK = new Result();
        public static Result Error = new Result();
        public static implicit operator bool(Result result)
        {
            return result == OK;
        }
        public static implicit operator Result( bool b)
        {
            return b ? OK : Error;
        }
    }

You can use it like Enum or like bool, for example
var x = Result.OK;
Result y = true;
if(x) ...
or
if(y==Result.OK)

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