If I want to rename an interface without breaking existing code, would this work?
The old interface:
namespace RenameInterfaceNicely
{
/// <summary>
/// The old name of the interface.
/// </summary>
[Obsolete("IFoo is replaced by IFooNew.")]
public interface IFoo
{
void Test();
}
}
The new, renamed, interface:
namespace RenameInterfaceNicely
{
#pragma warning disable 0618
/// <summary>
/// IFooNew new name for the interface IFoo.
/// </summary>
public interface IFooNew : IFoo
{
}
#pragma warning restore 0618
}
So if I have a class Foo that used the old interface, that I now change to use the new interface. Will users of Foo get in trouble?
Foo before the change:
public class Foo : IFoo
{
public void Test()
{
// Do something
return;
}
}
Foo after the change:
public class Foo : IFooNew
{
public void Test()
{
// Do something
return;
}
}
Example of existing code:
// Old code
IFoo oldFoo = new Foo();
oldFoo.Test();
IFoo oldFoo2 = new Bar().GetFoo();
...
Bar bar = new Bar();
bar.DoSomethingWithFoo(oldFoo);
Example of new code:
// New code
IFooNew newFoo = new Foo();
newFoo.Test();
IFooNew newFoo2 = new Bar().GetFoo();
...
Bar bar = new Bar();
bar.DoSomethingWithFoo(newFoo);
My question was inspired by this one: A definitive guide to API-breaking changes in .NET and the fact that someone made a breaking change by renaming some interfaces that I was using.