Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

c# - Does adding enum values break binary compatibility?

Imagine this enum in a DLL.

public enum Colors
{
    Red,
    Green
}

Does adding enum values break binary compatibility? If I were to change it, would existing EXEs break?

public enum Colors
{
    Red,
    Green,
    Blue
}

I saw this answer, but it seemed to address the case of inserting a value. If I add values to the end only, is that OK?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No, this doesn't break binary compatibility (in as much as: the assembly will still load etc), because enums are basically integer literal constants. Inserting values in the middle is obviously a really dangerous idea, but you've already excluded that.

However, it can cause a number of other issues that you need to guard against:

  • some code (switch statements in particular) may not anticipate the new values; technically this was an issue before too, since enums are not value-checked (enum variables can contain undefined values)
  • anything that queries the available enums is going to get different results
    • in particular, serialization and deserialization may fail unexpectedly if there is data using enums that aren't yet expected by the particular client

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...