相关疑难解决方法(0)

如何将枚举设置为null

我有一个枚举

string name;

public enum Color
{
  Red,
  Green,
  Yellow
}
Run Code Online (Sandbox Code Playgroud)

如何在加载时将其设置为NULL.

name = "";
Color color = null; //error
Run Code Online (Sandbox Code Playgroud)

编辑:我的不好,我没有正确解释.但所有与可空的相关的答案都是完美的.我的情况是如果,我已经在类中使用其他元素(例如名称等)获取/设置枚举.在页面加载时,我初始化类并尝试将值默认为null.这是场景(代码在C#中):

namespace Testing
{
    public enum ValidColors
    {
        Red,
        Green,
        Yellow
    }

    public class EnumTest
    {
        private string name;
        private ValidColors myColor;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public ValidColors MyColor
        {
            get { return myColor; }
            set { myColor = value; }
        }

    }

    public partial class _Default : …
Run Code Online (Sandbox Code Playgroud)

c# null enums nullable

172
推荐指数
5
解决办法
20万
查看次数

标签 统计

c# ×1

enums ×1

null ×1

nullable ×1