简体   繁体   中英

Will hiding a property in a base class cause a breaking change?

I have a base class 'A' (a collection) with an ArrayList and an indexed property that returns that index in the ArrayList. I also have a derived class 'B' with a generic type T, that acts as a collection of T. Finally, there are classes 'C' - 'F' that derive the class B with each having a different T.

The code looks something like the following:

public class A 
{
    protected ArrayList list;
    public object this[int index] { get { return list[index]; } }
}


public class B<T> : A
{

}

public class C : B<G>
{

}

There are 3rd party .dlls using these classes, so I'm wondering if I can make a change without breaking those assemblies. I want to add an indexed property to B that will hide the indexed property in A.

public class B<T> : A
{
    // Adding this property
    public new <T> this[int index] { get { return (T)list[index]; } }
}

Now the other .dlls may have code that looks something like the following:

C c = new C();
G g = (c[0] as G);

The previous code with using 'as' to cast will still compile with the new index property, but it will also allow for the code to be simplified to this:

C c = new C();
G g = c[0];

I'm wondering if this addition of the indexed property to B would break the 3rd party .dlls using my assembly.

I would also really appreciate an explanation of why or why not it is or isn't a breaking change.

I figured out the answer to my question. This will cause a binary-level break in a certain circumstance.

If I make a property in the derived class which hides the property in the base class, 3rd party .dlls being compiled with this change will no longer function with older versions of my .dll. The new .dlls would be compiled with a reference to the property in the derived class and attempt to use that property, which would not exist in the old version of my .dll.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM