title: A definitive guide to API-breaking changes in .NET
body: <p>I would like to gather as much information as possible regarding API versioning in .NET/CLR, and specifically how API changes do or do not break client applications. First, let's define some terms:</p>
  
  <p><strong>API change</strong> - a change in the publicly visible definition of a type, including any of its public members. This includes changing type and member names, changing base type of a type, adding/removing interfaces from list of implemented interfaces of a type, adding/removing members (including overloads), changing member visibility, renaming method and type parameters, adding default values for method parameters, adding/removing attributes on types and members, and adding/removing generic type parameters on types and members (did I miss anything?). This does not include any changes in member bodies, or any changes to private members (i.e. we do not take into account Reflection).</p>
  
  <p><strong>Binary-level break</strong> - an API change that results in client assemblies compiled against older version of the API potentially not loading with the new version. Example: changing method signature, even if it allows to be called in the same way as before (ie: void to return type / parameter default values overloads).</p>
  
  <p><strong>Source-level break</strong> - an API change that results in existing code written to compile against older version of the API potentially not compiling with the new version. Already compiled client assemblies work as before, however. Example: adding a new overload that can result in ambiguity in method calls that were unambiguous previous.</p>
  
  <p><strong>Source-level quiet semantics change</strong> - an API change that results in existing code written to compile against older version of the API quietly change its semantics, e.g. by calling a different method. The code should however continue to compile with no warnings/errors, and previously compiled assemblies should work as before. Example: implementing a new interface on an existing class that results in a different overload being chosen during overload resolution.</p>
  
  <p>The ultimate goal is to catalogize as many breaking and quiet semantics API changes as possible, and describe exact effect of breakage, and which languages are and are not affected by it. To expand on the latter: while some changes affect all languages universally (e.g. adding a new member to an interface will break implementations of that interface in any language), some require very specific language semantics to enter into play to get a break. This most typically involves method overloading, and, in general, anything having to do with implicit type conversions. There doesn't seem to be any way to define the "least common denominator" here even for CLS-conformant languages (i.e. those conforming at least to rules of "CLS consumer" as defined in CLI spec) - though I'll appreciate if someone corrects me as being wrong here - so this will have to go language by language. Those of most interest are naturally the ones that come with .NET out of the box: C#, VB and F#; but others, such as IronPython, IronRuby, Delphi Prism etc are also relevant. The more of a corner case it is, the more interesting it will be - things like removing members are pretty self-evident, but subtle interactions between e.g. method overloading, optional/default parameters, lambda type inference, and conversion operators can be very surprising at times.</p>
  
  <p>A few examples to kickstart this:</p>
  
  <h2>Adding new method overloads</h2>
  
  <p>Kind: source-level break</p>
  
  <p>Languages affected: C#, VB, F#</p>
  
  <p>API before change:</p>
  
  <pre><code>public class Foo
  {
      public void Bar(IEnumerable x);
  }
  </code></pre>
  
  <p>API after change:</p>
  
  <pre><code>public class Foo
  {
      public void Bar(IEnumerable x);
      public void Bar(ICloneable x);
  }
  </code></pre>
  
  <p>Sample client code working before change and broken after it:</p>
  
  <pre><code>new Foo().Bar(new int[0]);
  </code></pre>
  
  <h2>Adding new implicit conversion operator overloads</h2>
  
  <p>Kind: source-level break.</p>
  
  <p>Languages affected: C#, VB</p>
  
  <p>Languages not affected: F#</p>
  
  <p>API before change:</p>
  
  <pre><code>public class Foo
  {
      public static implicit operator int ();
  }
  </code></pre>
  
  <p>API after change:</p>
  
  <pre><code>public class Foo
  {
      public static implicit operator int ();
      public static implicit operator float ();
  }
  </code></pre>
  
  <p>Sample client code working before change and broken after it:</p>
  
  <pre><code>void Bar(int x);
  void Bar(float x);
  Bar(new Foo());
  </code></pre>
  
  <p>Notes: F# is not broken, because it does not have any language level support for overloaded operators, neither explicit nor implicit - both have to be called directly as <code>op_Explicit</code> and <code>op_Implicit</code> methods.</p>
  
  <h2>Adding new instance methods</h2>
  
  <p>Kind: source-level quiet semantics change.</p>
  
  <p>Languages affected: C#, VB</p>
  
  <p>Languages not affected: F#</p>
  
  <p>API before change:</p>
  
  <pre><code>public class Foo
  {
  }
  </code></pre>
  
  <p>API after change:</p>
  
  <pre><code>public class Foo
  {
      public void Bar();
  }
  </code></pre>
  
  <p>Sample client code that suffers a quiet semantics change:</p>
  
  <pre><code>public static class FooExtensions
  {
      public void Bar(this Foo foo);
  }
  
  new Foo().Bar();
  </code></pre>
  
  <p>Notes: F# is not broken, because it does not have language level support for <code>ExtensionMethodAttribute</code>, and requires CLS extension methods to be called as static methods. </p>