Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should enums have uninitialized values .

Tags:

c#

enums

We were having a debate if enums should have uninitialized values. For example. We have

public enum TimeOfDayType
{
   Morning
   Afternoon
   Evening
}

or

public enum TimeOfDayType
{
   None
   Morning
   Afternoon
   Evening
}

I think that there shouldn't be any none but then you have to default to some valid value on initialization. But others thought there should be some indication of uniitized state by having another enum that is None or NotSet.

thoughts?

like image 606
leora Avatar asked Dec 06 '08 02:12

leora


1 Answers

Speaking of nullable types - I think they can be used to solve the problem of forcing/not forcing the initialization of an enum. Say we have

enum Color { Red, Blue }

And let's say you have a function:

void Draw(Color c);

That function says that it requires a valid Color. However, we could also have this function:

void Draw(Color? c);

That says that the function can handle not being passed a color (null would be passed to indicate "don't care").

Well, it's one alternative to None members.

like image 127
Frank Krueger Avatar answered Sep 28 '22 22:09

Frank Krueger