I have a class with an enum property like so:
public class Foo
{
public Color ColorType {get;set;}
}
public enum Color
{
Red,
Green,
}
Now this class can be initialized like so:
var foo = new Foo();
without the ColorType property ever being set. Now, I'm trying to create a method and perform actions on whether that enum was ever set or not, for example I have a method
private void checkEnum(Foo foo)
{
if(foo.ColorType !=null)
{
//perform these actions
}else
{
//perform those actions
}
}
however I get a warning saying that value will never be null and upon further research, if the enum is never set if will default to the first value which would be Red in my case, I was thinking about adding a value to my enum which would be 'not set' and make that value the first one, so if it hasnt been set then the enum will have the value 'not set', is there a better way of doing this, my proposed method seems like it could get messy
In C#, we can check the specific type is enum or not by using the IsEnum property of the Type class. It will return true if the type is enum. Otherwise, this property will return false. It is a read-only property.
The default value of an enum E is the value produced by the expression (E)0 .
An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).
Initializing Enum Constants. Enums in Kotlin, just like in Java, can have a constructor. Since enum constants are instances of an Enum class, the constants can be initialized by passing specific values to the constructor.
You can use one of two methods: default enum value or a nullable enum.
Default enum value
Since an enum is backed by an integer, and int
defaults to zero, the enum will always initialize by default to the value equivalent to zero. Unless you explicitly assign enum values, the first value will always be zero, second will be one, and so on.
public enum Color { Undefined, Red, Green } // ... Assert.IsTrue(Color.Undefined == 0); // success!
Nullable enum
The other way to handle unassigned enum is to use a nullable field.
public class Foo { public Color? Color { get; set; } } // ... var foo = new Foo(); Assert.IsNull(foo.Color); // success!
You can make it so that the underlying private field is nullable, but the property is not.
E.g.
class SomeClass
{
private Color? _color; // defaults to null
public Color Color
{
get { return _color ?? Color.Black; }
set { _color = value; }
}
public bool ColorChanged
{
get { return _color != null; }
}
}
That way if color == null
you know it hasn't been set yet, and you are also stopping the user from setting it to null
(or undefined
as other answers specify). If color
is null
you have 100% certainty that the user has not set it ever.
Only catch is the default value returned by the get
but you could always throw an excepting if it better matches your program.
You can also take it one step further by making it so that the set
only sets the field if the given value is not equal to the default value (depending on your use case):
public Color Color
{
get { return _color ?? Color.Black; }
set
{
if (value != Color)
{
_color = value;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With