根据 C# 中的条件将枚举值分配给枚举
我有一个类,并在其中声明了一个枚举,就像
public enum file_type {readonly, readwrite, system}
现在基于一个条件,我想将枚举 file_type 设置为一个值,例如
if("file is markedreadonly")
file_type = readonly;
是否可以在 c# 中这样做
i have a class and have declared an enum in it like
public enum file_type {readonly, readwrite, system}
Now based on a condition i want to set the enum file_type to a value something like
if("file is markedreadonly")
file_type = readonly;
is it possible to do so in c#
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的语法是错误的。 file_type 是一种类型,而不是变量。 另外,您需要 @readonly,因为 readonly 是保留字。
Your syntax is wrong. file_type is a type, not a variable. Also, you need @readonly, since readonly is a reserved word.
枚举本身的行为更像类型。 您需要一个file_type类型的变量,然后可以将其分配给file_type.readonly
The enums themselves behave more like types. You need a variable of type file_type which you can then assign to file_type.readonly
你绝对可以做到这一点,几乎完全按照你的指示。 注意:我更改了大小写以符合公认的 C# 编码指南。
另外,请查看 System.IO.FileAccess 和 System.IO.FileAttribute 枚举。 他们可能会为您提供更合适的标准实现。 就我个人而言,我更喜欢使用系统类型而不是“滚动我自己的”——工作量更少并且更容易被其他程序员理解。
华泰
You can definitely do this, almost exactly as you indicated. Note: I've changed casing to conform to accepted C# coding guidelines.
Also, check out System.IO.FileAccess and System.IO.FileAttribute enums. They may provide you with a standard implementation that is more suitable. Personally, I prefer to use a system type rather than "rolling my own" -- less work and more easily understood by other programmers.
HTH
通过编写 file_type = readonly,您试图在运行时更改 Enum 的定义,这是不允许的。
创建 file_type 类型的变量,然后将其设置为只读。
另外,请使用 .NET 命名标准来命名您的变量和类型。 此外,对于枚举,建议将“None”枚举作为第一个值。
By writing file_type = readonly, you are trying to change the definition of an Enum at runtime, which is not allowed.
Create variable of type file_type and then set it to readonly.
Also, please use .NET naming standards to name your variable and types. Additionally for Enums, it is recommended to have a 'None' enum as the first value.