我有一个enum
string name;
public enum Color
{
Red,
Green,
Yellow
}
如何将它设置为NULL加载。
name = "";
Color color = null; //error
编辑:
我的错,我没有解释清楚。但所有与nullable相关的答案都是完美的。我的情况是什么,如果,我有获得/设置enum类中的其他元素,如名称等。在页面加载,我初始化类,并尝试默认值为空。下面是场景(代码是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 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
EnumTest oEnumTest = new EnumTest();
oEnumTest.Name = "";
oEnumTest.MyColor = null; //???
}
}
}
然后使用下面的建议,我修改了上面的代码,使其与get和set方法一起工作。我只需要在EnumTest类中声明私有枚举变量和get/set方法中添加“?”:
public class EnumTest
{
private string name;
private ValidColors? myColor; //added "?" here in declaration and in get/set method
public string Name
{
get { return name; }
set { name = value; }
}
public ValidColors? MyColor
{
get { return myColor; }
set { myColor = value; }
}
}
谢谢大家的建议。
使用Nullable enum作为类/方法变量和类属性时的一些观察和问题:
在c# v7.3(使用VS2019在.NET v4.7.2上测试)中,
如此:
Color? color = null; // works
但这不是:
Color? color = condition ? Color.Red : null;
// Error CS8370
// Feature 'target-typed conditional expression' is not available in C# 7.3.
// Please use language version 9.0 or greater.
修复是类型转换为null:
Color? color = condition ? Color.Red : (Color?)null; // works
此外,将变量命名为Color,即与类型完全相同的情况,仅当类型是非nullable时才有效:
Color Color = condition ? Color.Red : Color.None; // works fine!
但是当类型为Nullable时失败:
Color? Color = condition ? Color.Red : (Color?)null;
// Error CS0165 Use of unassigned local variable 'Color'
// Error CS1061 'Color?' does not contain a definition for 'Red'
// and no accessible extension method 'Red' accepting a first argument of type 'Color?'
// could be found (are you missing a using directive or an assembly reference?)
修复方法是使用命名空间的类型的完全限定名:
Color? Color = condition ? MyEnums.Color.Red : (Color?)null; // works now
但是最好还是坚持用小写字母命名变量的标准做法:
Color? color = condition ? Color.Red : (Color?)null; // works now
在类级别的属性中也可以看到类似的问题:
class Program {
Color? Color => Condition ? Color.Red : (Color?)null;
}
// Error CS1061 'Color?' does not contain a definition for 'Red'
// and no accessible extension method 'Red' accepting a first argument of type 'Color?'
// could be found (are you missing a using directive or an assembly reference?)
同样,修复是完全限定类型:
Color? Color => Condition ? MyEnums.Color.Red : (Color?)null; // works now
使用Nullable enum作为类/方法变量和类属性时的一些观察和问题:
在c# v7.3(使用VS2019在.NET v4.7.2上测试)中,
如此:
Color? color = null; // works
但这不是:
Color? color = condition ? Color.Red : null;
// Error CS8370
// Feature 'target-typed conditional expression' is not available in C# 7.3.
// Please use language version 9.0 or greater.
修复是类型转换为null:
Color? color = condition ? Color.Red : (Color?)null; // works
此外,将变量命名为Color,即与类型完全相同的情况,仅当类型是非nullable时才有效:
Color Color = condition ? Color.Red : Color.None; // works fine!
但是当类型为Nullable时失败:
Color? Color = condition ? Color.Red : (Color?)null;
// Error CS0165 Use of unassigned local variable 'Color'
// Error CS1061 'Color?' does not contain a definition for 'Red'
// and no accessible extension method 'Red' accepting a first argument of type 'Color?'
// could be found (are you missing a using directive or an assembly reference?)
修复方法是使用命名空间的类型的完全限定名:
Color? Color = condition ? MyEnums.Color.Red : (Color?)null; // works now
但是最好还是坚持用小写字母命名变量的标准做法:
Color? color = condition ? Color.Red : (Color?)null; // works now
在类级别的属性中也可以看到类似的问题:
class Program {
Color? Color => Condition ? Color.Red : (Color?)null;
}
// Error CS1061 'Color?' does not contain a definition for 'Red'
// and no accessible extension method 'Red' accepting a first argument of type 'Color?'
// could be found (are you missing a using directive or an assembly reference?)
同样,修复是完全限定类型:
Color? Color => Condition ? MyEnums.Color.Red : (Color?)null; // works now