
{"id":1397,"date":"2014-01-27T11:16:16","date_gmt":"2014-01-27T16:16:16","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=1397"},"modified":"2014-01-27T11:16:16","modified_gmt":"2014-01-27T16:16:16","slug":"changing-order-of-enum-values-in-c-breaks-binary-compatibility-badly","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=1397","title":{"rendered":"Changing order of enum values in C# breaks binary compatibility badly"},"content":{"rendered":"<p>Suppose I have a public interface that uses this enum:<\/p>\n<pre><code>public enum LogLevel \/\/ v1\n{\n    Info,\n    Warning,\n    Error,\n    Debug\n}\n<\/code><\/pre>\n<p>There is a problem with this enum: the values are not in the order of increasing severity. Let&#8217;s say we want to fix this like so:<\/p>\n<pre><code>public enum LogLevel \/\/ v2\n{\n    Debug,\n    Info,\n    Warning,\n    Error\n}\n<\/code><\/pre>\n<p>It will definitely break backward compatibility, because numeric equivalents of the enum values will change. But how much of an effect will it have?<\/p>\n<p>Let&#8217;s say, we have an assembly compiled with the new version of the enum:<\/p>\n<pre><code>public class Log\n{\n    public static void ShowLevel(LogLevel level)\n    {\n        Console.WriteLine(\"Log received \" + level);\n    }\n}\n<\/code><\/pre>\n<p>and we call it from an assembly compiled with the old version of the enum:<\/p>\n<p><code>Log.ShowLevel(LogLevel.Error);<\/code><\/p>\n<p>If <code>LogLevel.Error<\/code> is passed as a symbolic constant like in Java, we should be fine. If it is passed as a number like in C, the results won&#8217;t be pretty.<\/p>\n<p>The experiment shows, that the result is passed as a number, like in C:<\/p>\n<p><code>Using enum provider version 2.0<br \/>\nPassing to Log: LogLevel.Error<br \/>\nLog received Warning<\/code><\/p>\n<p>See attached code for details: <a href=\"http:\/\/www.ikriv.com\/\/blog\/wp-content\/uploads\/2014\/01\/ChangeEnumValue.7z\">ChangeEnumValue.7z<\/a>.<br \/>\nConclusion: <b><font color=\"red\">do not change the order of enum values in C#, it will break the clients badly<\/font><\/b>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Suppose I have a public interface that uses this enum: public enum LogLevel \/\/ v1 { Info, Warning, Error, Debug } There is a problem with this enum: the values <a href=\"https:\/\/ikriv.com\/blog\/?p=1397\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[8,16],"tags":[],"class_list":["entry","author-ikriv","post-1397","post","type-post","status-publish","format-standard","category-cs","category-demos"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1397"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1397\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}