Does the number of renames change the break?

Consider the following code:

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

Will OTH = 5 change to OTH = 7 violation?


Edit: I never save an int value, just a textual representation of the enumeration. It can be used in other DLLs, but it will use the same storage.

+6
enums c #
source share
6 answers

This is a modified change since you are changing the public API.

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

From MSDN - enum (C # link) :

As with any constant, all references to individual enumeration values ​​are converted to numeric literals at compile time. This can create potential versioning issues, as described in the book "Constants" (C # Programming Guide).

+12
source share

It depends on whether you have control over the complete code for your solution or if other users are exporting the library.

  • Perhaps not if all of the following statements are true
    • If it's just for your own use
    • You restore everything
    • You only use enumeration
    • You do not store the ordinal, do not add it to / from it, save it in the database.
  • Yes , if one of the following conditions is true :
    • If someone else uses your library and does not recompile or use the link to a specific version (provided that you increase the version of the assembly) or a signed link. Another code will save its own copy of the ordinal value, which now does not match.
    • You are using explicit casts
    • You serialize the data and want to use the old "save-files"

There are similar gotchas with publicly exposed consts.

In general, suppose yes - this is an amazing change!

+3
source share

Will be. Suppose you store some data as an int from an enumeration, retrieving data in the future will lead to incorrect results. In this case, the data stored for OTH until the change will not be displayed as OTH when saving 5 for OTH at present, and you will get 5 in the future, and you need 7 for the same.

+2
source share

Although it is true that this is not a continuous change, if no one depends on its value equal to 5, it is worth noting that anyone who uses it depends on the fact that it is equal to 5, even if it is not explicit in their code.

If I write code for this assembly, for example:

 if(myVal == SomeCode.OTH) { //do something } 

Then internally, comparing it with a value of 5. Even if number 5 is not displayed anywhere in my source code, so it will be broken if I build a new version of the assembly.

The grace of saving is that I do not need to rewrite to cope with this change, just recompile.

+2
source share

Firstly, both SSN and OTH = 5 is a typo, because otherwise is it a compile-time error for the switch statement using this enumeration? Now, changing OTH = 5 to OTH = 7, this would change the change if you use enum as SomeCode someCode = (SomeCode)5 .

0
source share

It depends. This is an inextricable change only if you can make sure that no one depends on its value of 5 or the same as the SSN, or if you can reorganize such a dependency.

0
source share

All Articles