Sri*_*ddy 172 c# null enums nullable
我有一个枚举
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 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
EnumTest oEnumTest = new EnumTest();
oEnumTest.Name = "";
oEnumTest.MyColor = null; //???
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用下面的建议我更改了上面的代码,使其适用于get和set方法.我只需要添加"?" 在声明私有枚举变量和get/set方法期间在EnumTest类中:
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢大家的可爱建议.
Rod*_*ley 338
你可以使用"?" 可操作类型的运算符.
public Color? myColor = null;
Run Code Online (Sandbox Code Playgroud)
或者通过使枚举(即0)中的FIRST值成为默认值,使用不能为null的枚举的标准实践.例如,在颜色无的情况下.
public Color myColor = Color.None;
Run Code Online (Sandbox Code Playgroud)
Tim*_*son 41
如果这是C#,它将不起作用:枚举是值类型,而不是null.
正常选项是添加None成员:
public enum Color
{
None,
Red,
Green,
Yellow
}
Color color = Color.None;
Run Code Online (Sandbox Code Playgroud)
......或使用Nullable:
Color? color = null;
Run Code Online (Sandbox Code Playgroud)
Cj *_* S. 23
使你的变量可以为空.喜欢:
Color? color = null;
Run Code Online (Sandbox Code Playgroud)
要么
Nullable<Color> color = null;
Run Code Online (Sandbox Code Playgroud)
枚举是C#中的"值"类型(意味着枚举存储为它的任何值,而不是作为对存储值本身的内存中的位置的引用).您不能将值类型设置为null(因为null仅用于引用类型).
也就是说,您可以使用Nullable<T>包含值类型的内置类,以便您可以将它们设置为null,检查它是否HasValue得到它的实际值Value.(这些都是Nullable<T>对象的方法.
name = "";
Nullable<Color> color = null; //This will work.
Run Code Online (Sandbox Code Playgroud)
您还可以使用以下快捷方式:
Color? color = null;
Run Code Online (Sandbox Code Playgroud)
那是一样的Nullable<Color>;
使用类/方法变量和类属性时的一些观察结果和陷阱:Nullable enum
在(用C# v7.3测试)中,.NET v4.7.2VS2019
这有效:
Color? color = null; // works
Run Code Online (Sandbox Code Playgroud)
但这并没有:
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.
Run Code Online (Sandbox Code Playgroud)
修复方法是进行类型转换null:
Color? color = condition ? Color.Red : (Color?)null; // works
Run Code Online (Sandbox Code Playgroud)
此外,命名变量Color(即与类型完全相同的大小写)仅在类型为非 Nullable 时才有效:
Color Color = condition ? Color.Red : Color.None; // works fine!
Run Code Online (Sandbox Code Playgroud)
但当类型为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?)
Run Code Online (Sandbox Code Playgroud)
修复方法是使用带有命名空间的类型的完全限定名称:
Color? Color = condition ? MyEnums.Color.Red : (Color?)null; // works now
Run Code Online (Sandbox Code Playgroud)
但最好坚持变量小写命名的标准做法:
Color? color = condition ? Color.Red : (Color?)null; // works now
Run Code Online (Sandbox Code Playgroud)
对于 -level 的属性也可以看到类似的问题class:
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?)
Run Code Online (Sandbox Code Playgroud)
同样,修复方法是完全限定类型:
Color? Color => Condition ? MyEnums.Color.Red : (Color?)null; // works now
Run Code Online (Sandbox Code Playgroud)