I’m porting some Java code to C#, and I ran into this idiom used to copy objects:
class Base
{
int x;
public Base(int x) { this.x = x; }
protected Base(Base other) { x = other.x; }
}
class Derived : Base
{
Base foo;
public Derived(Derived other)
: base(other)
{
foo = new Base(other.foo); // Error CS1540
}
}
Error CS1540 being:
Cannot access protected member ‘Base.Base(Base)’ via a qualifier of type ‘Base’; the qualifier must be of type ‘Derived’ (or derived from it)
I understand the purpose of this error: it prevents accessing protected members of sibling types. But Base.Base(Base) is obviously not going to be invoked on a sibling type! Is this simply not included in the spec, or am I missing some reason why this would not be safe?
EDIT: Gah, the idiom was new Base(other.foo) not new Base(other)
its not accessible you can check this post for details : Many Questions: Protected Constructors