Does changing the return type of a function for a child type breaks binary compatibility?

∥☆過路亽.° 提交于 2019-12-20 02:31:00

问题


Let's go straight to it :

Old code :

public interface IFoo {}
public class Foo : IFoo {}
...
public static IFoo Bar() { return new Foo(); }

New code :

public static Foo Bar() { return new Foo(); }

Obviously there should be no problem here, everything you were doing on the old return type, you can still do on the new return type, any is, as or cast should behave the same as before...

So did I break binary compatibility, or can I just release it as a minor version without bothering users ?


回答1:


This breaks binary compatibility, but not (most) compile-time compatibility issues, so it's typically an easy migration.

Note that it can even be a compile-time break if the client code constructs a delegate from the method.




回答2:


You could have problems with people who have created unit tests around your code. Because it's static people could have created a FooAdapater as follows:-

public class FooAdapater(){ 
public IFoo GetFoo() { Return your static Bar; }
}

They could then create a mocked foo adapter than returns a mocked foo. Your code would break this scenario.

So, no it's not binary compatible :-)



来源:https://stackoverflow.com/questions/14588558/does-changing-the-return-type-of-a-function-for-a-child-type-breaks-binary-compa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!