Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is changing the number of an enum a breaking change?

Tags:

c#

enums

Consider the following code:

public enum SomeCode
{
     NIF = 0
    ,NIE = 1
    ,CIF = 2
    ,PAS = 3
    ,NIN = 4
    ,SSN = 5
    ,OTH = 5
    ,UKN = 6
}

Would changing OTH = 5 to OTH = 7 be a breaking change?


Edit: I never store the int value, only ever the text representation of the enum. It may be used in other DLLs, but will use the same storage.

like image 618
cjk Avatar asked Aug 06 '10 08:08

cjk


2 Answers

It is a breaking change, as you are changing a public API.

Libraries/applications that were built with the old value will still hold the old value and use it. You will need to recompile them all.

From MSDN - enum (C# Reference):

Just as with any constant, all references to the individual values of an enum are converted to numeric literals at compile time. This can create potential versioning issues as described in Constants (C# Programming Guide).

like image 87
Oded Avatar answered Sep 19 '22 12:09

Oded


It depends upon whether you have control over the complete code for your solution or whether you are exporting a library to be used by others.

  • Maybe Not if all of the following are true
    • If it is just for your own use
    • You rebuild all
    • You only use the enumeration
    • You don't store the ordinal, cast to/from it, persist in a database.
  • Yes if any of the following are true
    • If someone else is using your library and doesn't recompile, and doesn't use a version specific reference (assuming you're incrementing your build version) or a signed reference. The other code will have saved its own copy of the ordinal value, which doesn't now match.
    • You use explicit casts against the ordinal value
    • You serialize the data and want to use the old "save-files"

There are similar gotchas with publicly exposed consts.

In general, assume yes - it is a breaking change!

like image 39
Ray Hayes Avatar answered Sep 19 '22 12:09

Ray Hayes