C#枚举类型安全 [英] C# enum type-safety

查看:83
本文介绍了C#枚举类型安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来强制C#枚举只接受几个明确命名常量之一,或者是有其他功能呢? C#的引用有这种事后的:

Is there a way to force the C# enum to only accept one of the several explicitly named constants, or is there another feature that does? The C# reference has this afterthought:

这是可能的任意整数值赋给一个枚举类型。
但是,你不应该这样做,因为隐含的预期是
,一个枚举变量将只持有由
枚举定义的值之一。为任意值分配给枚举
类型的变量是引入错误的高风险。

It is possible to assign any arbitrary integer value to an enum type. However, you should not do this because the implicit expectation is that an enum variable will only hold one of the values defined by the enum. To assign an arbitrary value to a variable of an enumeration type is to introduce a high risk for errors.

(A新语言的目的是,让这种草率的,这是令人费解给我。)

(A new language was designed that allows this sort of sloppiness. That is baffling to me.)

推荐答案

据我所知,你不能容许枚举和整数之间的转换停止C#。

As far as I know, you can't stop C# from allowing conversions between enums and integers.

作为一种解决方法,你也可以使用受限制的实例化自定义类型。该使用会看起来很相似,但你也将有机会来定义这种类型的方法和运算符

As a workaround, you could instead use a custom type with restricted instantiation. The usage will look similar, but you will also have the opportunity to define methods and operators for this type.

假设你有此枚举:

enum DayOfWeek
{
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday 
}

您可以在它的位置使用密封类。的优点是,你得到比较自由(因为一个值比较和参考比较是等价为这种情况下)。缺点是(像在C#中所有引用类型),它是可空的。

You could use a sealed class in its place. The advantage is that you get comparisons for free (because a value comparison and a reference comparison are equivalent for this case). The disadvantage is that (like all reference types in C#), it's nullable.

sealed class DayOfWeek
{
    public static readonly DayOfWeek Monday = new DayOfWeek(0);
    public static readonly DayOfWeek Tuesday = new DayOfWeek(1);
    public static readonly DayOfWeek Wednesday = new DayOfWeek(2);
    public static readonly DayOfWeek Thursday = new DayOfWeek(3);
    public static readonly DayOfWeek Friday = new DayOfWeek(4);
    public static readonly DayOfWeek Saturday = new DayOfWeek(5);
    public static readonly DayOfWeek Sunday = new DayOfWeek(6);

    private readonly int _value;

    private DayOfWeek(int value) 
    {
        _value = value;
    }
}



或者你可以使用一个结构。其优点是,它不能为空,因此它更类似于一个枚举。缺点是,你必须手动执行比较代码:

Or you could use a struct. The advantage is that it's not nullable and thus it's even more similar to an enum. The disadvantage is that you have to implement the comparison code manually:

struct DayOfWeek
{
    public static readonly DayOfWeek Monday = new DayOfWeek(0);
    public static readonly DayOfWeek Tuesday = new DayOfWeek(1);
    public static readonly DayOfWeek Wednesday = new DayOfWeek(2);
    public static readonly DayOfWeek Thursday = new DayOfWeek(3);
    public static readonly DayOfWeek Friday = new DayOfWeek(4);
    public static readonly DayOfWeek Saturday = new DayOfWeek(5);
    public static readonly DayOfWeek Sunday = new DayOfWeek(6);

    private readonly int _value;

    private DayOfWeek(int value)
    {
        _value = value;
    }

    public bool Equals(DayOfWeek other)
    {
        return _value == other._value;
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
        {
            return false;
        }
        return obj is DayOfWeek && Equals((DayOfWeek)obj);
    }

    public override int GetHashCode()
    {
        return _value;
    }

    public static bool operator ==(DayOfWeek op1, DayOfWeek op2)
    {
        return op1.Equals(op2);
    }

    public static bool operator !=(DayOfWeek op1, DayOfWeek op2)
    {
        return !(op1 == op2);
    }
}

这篇关于C#枚举类型安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆