Extensions methods and forward compatibilty of source code.

Posted by TcKs on Stack Overflow See other posts from Stack Overflow or by TcKs
Published on 2010-03-17T16:18:39Z Indexed on 2010/03/17 16:21 UTC
Read the original article Hit count: 173

Hi,

I would like solve the problem (now hypothetical but propably real in future) of using extension methods and maginification of class interface in future development.

Example:

/* the code written in 17. March 2010 */
public class MySpecialList : IList<MySpecialClass> {
    // ... implementation
}
// ... somewhere elsewhere ...
MySpecialList list = GetMySpecialList(); // returns list of special classes
var reversedList = list.Reverse().ToList(); // .Reverse() is extension method
/* now the "list" is unchanged and "reveresedList" has same items in reversed order */

/* --- in future the interface of MySpecialList will be changed because of reason XYZ*/

/* the code written in some future */
public class MySpecialList : IList<MySpecialClass> {
    // ... implementation
    public MySpecialList Reverse() {
        // reverse order of items in this collection
        return this;
    }
}
// ... somewhere elsewhere ...
MySpecialList list = GetMySpecialList(); // returns list of special classes
var reversedList = list.Reverse().ToList(); // .Reverse() was extension method but now is instance method and do something else !
/* now the "list" is reversed order of items and "reveresedList" has same items lake in "list" */

My question is: Is there some way how to prevent this case (I didn't find them)? If is now way how to prevent it, is there some way how to find possible issues like this? If is now way how to find possible issues, should I forbid usage of extension methods?

Thanks.

© Stack Overflow or respective owner

Related posts about extension-methods

Related posts about language-features