I have an enum:
[DataContract]
public enum Relationship
{
Spouse = 4,
ResidesWith = 1,
Parent = 2,
Other = 3,
PersonalGuarantor = 5,
CoApplicant = 6
}
As you can see, zero is not defined. I built my program around the idea that zero would never be defined in enums. That allows me to see which ones have been set from various comboBoxes, and which ones were set to a null value. If I set zero as the null value, there is no way to tell those two things apart, and it essential that I be able to.
Due to the lack of a default state, I get an error when I try to serialize the values.
Is there a way to have my xml serialize skip enums that have no value, or a way to avoid those errors? I really do not want to introduce a default value.
You need to use 0 as an enum value – make it a value that is not valid and that you check for (as you are already).
Don’t forget that enumerations are based on an integer type (their base type), so they will always have a value (value types cannot be null) and will default to
0. Making this explicit will also make things clearer in your codebase.Protip: You can use negative values in enums as well.