Pavel Minaev
7/22/2008 4:35:00 PM
On Jul 22, 7:54 pm, wdudek <wdu...@newsgroup.nospam> wrote:
> I've seen 2 differnt approaches people take to interfaces and am just curious
> on some other opinions. For purposes of this post I am talking about adding a
> new method to an existing interface. The first is that an interface can
> never change, once it's "published" it is fixed in stone and if a change
> needs to be made you should create a new interface that possibly inherits
> from the old one. The second is that it is ok as long as the change does not
> break the existing contracts, which wouldn't happen if a new method was
> added, since they were not using it before they wouldn't need to be aware of
> it now.
The second approach is incorrect since the interface can be
implemented as well as used; in fact, it is the whole point of having
an interface! If you change the interface, all existing
implementations would immediately break. Just imagine what would
happen if Microsoft added, say, a new method to IEnumerable tomorrow.
If you do not want to allow users to implement your interfaces, then
interfaces are the wrong tool for the job. Use abstract classes with
internal constructors. Those can only be used but not derived from by
code outside of your assembly (and friend assemblies, as needed), so
there you can safely add new methods. Well, almost - keeping in mind
that adding a new overload can break existing code; if you had a
method Foo(double), and in your new version added an overload
Foo(decimal), any code that called it passing an integer - e.g.
Foo(123) - would now stop compiling because of ambiguity.