根据c#中的条件为枚举指定枚举值

Ani*_*oel 0 c# enums

我有一个课,并在其中声明了一个枚举

public enum file_type {readonly, readwrite, system}
Run Code Online (Sandbox Code Playgroud)

现在基于我想要将enum file_type设置为类似值的条件

if("file is markedreadonly")
   file_type = readonly;
Run Code Online (Sandbox Code Playgroud)

是不是可以在c#中这样做

Sol*_*ogi 6

通过编写file_type = readonly,您尝试在运行时更改Enum的定义,这是不允许的.

创建file_type类型的变量,然后将其设置为readonly.

另外,请使用.NET命名标准来命名变量和类型.此外,对于Enums,建议将'None'枚举作为第一个值.

public enum FileType { None, ReadOnly, ReadWrite, System}

FileType myFileType = FileType.None;

if( //check if file is readonly)
    myFileType = FileType.ReadOnly;
Run Code Online (Sandbox Code Playgroud)