Problem
I’ve created an enum.
string name;
public enum Color
{
Red,
Green,
Yellow
}
On load, how do I make it NULL?
name = "";
Color color = null; //error
Edited: My apologies for not explaining it correctly. However, all of the nullable solutions are correct. What if I have a get/set for an enum in a class with other elements such as name, etc.? I initialize the class on page load and try to set the values to null. The following is the situation (all code is written in 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; //???
}
}
}
Then I updated the above code to work with get and set methods using the advice below. All I have to do now is add “?” to the EnumTest class during the declaration of the private enum variable and the get/set method:
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; }
}
}
Thank you very much for all of your wonderful ideas.
Asked by Sri Reddy
Solution #1
For a nullable type, you can use the “?” operator.
public Color? myColor = null;
Alternatively, for enums that cannot be null, adopt the traditional practice of making the first value in the enum (aka 0) the default value. In the case of color None, for example.
public Color myColor = Color.None;
Answered by Rodney S. Foley
Solution #2
It won’t work if it’s written in C#: Enums are value types that cannot contain null values.
Adding a None member is the standard option:
public enum Color
{
None,
Red,
Green,
Yellow
}
Color color = Color.None;
Alternatively, you can use Nullable:
Color? color = null;
Answered by Tim Robinson
Solution #3
Create a nullable variable. Like:
Color? color = null;
or
Nullable<Color> color = null;
Answered by Cj S.
Solution #4
In C#, an enum is a “value” type (means the the enum is stored as whatever value it is, not as a reference to a place in memory where the value itself is stored). You can’t utilize null value types (since null is used for reference types only).
That stated, you can use the NullableT> class, which covers value types and allows you to change them to null, check if they HaveValue, and get their real Value. (Those are both NullableT> objects’ methods.)
name = "";
Nullable<Color> color = null; //This will work.
You may also use the following shortcut:
Color? color = null;
NullableColor> is the same as this.
Answered by Chris Pfohl
Solution #5
When working with Nullable enum as class/method variables and class properties, there are a few things to keep in mind:
C# v7.3 is the latest version of the programming language (tested on .NET v4.7.2 with VS2019),
this works:
Color? color = null; // works
but this doesn’t:
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.
The solution is to cast null to a different type:
Color? color = condition ? Color.Red : (Color?)null; // works
Furthermore, only when the type is non-Nullable does naming the variable Color, i.e. exactly the same case as the type, work:
Color Color = condition ? Color.Red : Color.None; // works fine!
When the type is Nullable, however, it fails miserably:
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?)
To solve the problem, use the type’s fully qualified name with the namespace:
Color? Color = condition ? MyEnums.Color.Red : (Color?)null; // works now
However, it’s advisable to stick to the traditional practice of naming variables in lowercase:
Color? color = condition ? Color.Red : (Color?)null; // works now
A class-level property has a similar problem:
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?)
The solution is to completely qualify the type:
Color? Color => Condition ? MyEnums.Color.Red : (Color?)null; // works now
Answered by SNag
Post is based on https://stackoverflow.com/questions/4337193/how-to-set-enum-to-null