在一个恒定的枚举循环定义 [英] Circular definition in a constant enum

查看:85
本文介绍了在一个恒定的枚举循环定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建类型的常枚举但我得到一个错误..
我的枚举是:

I'm trying to create a constant of type Enum but I get a error.. My enum is:

public enum ActivityStatus
{
    Open = 1,
    Close = 2
}

和我有一个使用它的模型:

and I have a model that uses it:

public class CreateActivity
{
    public int Id;
    public const ActivityStatus ActivityStatus = ActivityStatus.Open;
}



出现以下错误:

the following error occurs:

错误1'Help_Desk.Models.CreateActivity.ActivityStatus的恒定值的评估涉及循环定义...

Error 1 The evaluation of the constant value for 'Help_Desk.Models.CreateActivity.ActivityStatus' involves a circular definition...

但是,如果我更改 ActivityStatus 的name属性它的作品!

But if I change the name of ActivityStatus property it works!

public class CreateActivity
{
    public int Id;
    public const ActivityStatus AnyOtherName = ActivityStatus.Open;
}



为什么会发生?

Why it happens?

推荐答案

由于C#编译器intepretes第三 ActivityStatus

Because the c# compiler intepretes the third ActivityStatus in:

public const ActivityStatus ActivityStatus = ActivityStatus.Open; 



定义,而不是比枚举的名字不断存在的名称 - 因此循环引用:你definining恒定在不断本身的条款。

as the name of the constant being defined instead than the name of the enumeration - hence the circular reference: you are definining a constant in terms of the constant itself.

在C#中,你可以使用相同的名称成员和类型,通常解决歧义指定完全合格的名称(即添加命名空间),但我的经验是不是一个好主意,它使代码混乱:编译器可以找出哪个是哪个,但穷人的人类阅读代码中有一个很难搞清楚,如果某个名字指的是类或类型或成员。

In C# you can use the same name for members and types, and usually resolve ambiguities specifying the fully qualified names (i.e. adding the namespace), but in my experience it is not a good idea, it makes code confusing: the compiler can figure out which is which, but the poor human reading the code has an hard time figuring out if a certain name refers to a class or type or member.

这篇关于在一个恒定的枚举循环定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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