Consider the following hierarchy:
public abstract class Base<T>
{
protected abstract T N { get; set; }
}
public abstract class Intermediate<T> : Base<T>
{
protected override T N { get; set; }
}
public class Derived : Intermediate<int>
{
public Derived() { N = 1; }
protected override sealed int N { get; set; }
}
Running Visual Studio 2015 Professional code analysis gives a warning
Warning CA2214 'Derived.Derived()' contains a call chain that results in a call to a virtual method defined by the class. Review the following call stack for unintended consequences:
Derived..ctor()
Base<T>.set_N(T):Void
This seems like another question , but still different, as property in is Derivedmarked as sealed.
On the other hand, the code snippet below, without using any generics, does not cause any warnings:
public abstract class Base
{
protected abstract int N { get; set; }
}
public abstract class Intermediate : Base
{
protected override int N { get; set; }
}
public class Derived : Intermediate
{
public Derived() { N = 1; }
protected override sealed int N { get; set; }
}
Similarly, if I skip the intermediate class and instead get Derivedfrom Base, the warning disappears even in the general case. On the other hand, if I delete the modifier sealedfor the last example, the warning appears again, as expected.
So, does this work in the general case? And if so, what is an example where you need to be careful?