Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if an enum property has been set? C#

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

like image 313
user2341534 Avatar asked May 14 '13 23:05

user2341534


People also ask

How do you check if a property is an enum?

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.

Does enum have default value?

The default value of an enum E is the value produced by the expression (E)0 .

Can enums have properties?

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).

Can enum be initialized?

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.


2 Answers

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! 
like image 114
Michael Teper Avatar answered Oct 13 '22 15:10

Michael Teper


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;
        }
    }
}
like image 32
Sellorio Avatar answered Oct 13 '22 14:10

Sellorio