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

后端 未结 2 1315
北荒
北荒 2021-01-20 16:34

Let\'s go straight to it :

Old code :

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


        
2条回答
  •  不知归路
    2021-01-20 16:59

    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 :-)

提交回复
热议问题