logo
CSharp_Prog_Guide

Модификаторы доступа в переопределяющих методах доступа

При переопределении свойства или индексатора переопределенные методы доступа должны быть доступны переопределяющему коду. Кроме того, уровень доступности свойства/индексатора, а также методов доступа должен перехватывать соответствующее свойство/индексатор и методы доступа. Пример.

public class Parent

{

public virtual int TestProperty

{

// Notice the accessor accessibility level.

protected set { }

// No access modifier is used here.

get {return 0;}

}

}

public class Kid : Parent

{

public override int TestProperty

{

// Use the same accessibility level as in the overridden accessor.

protected set { }

// Cannot use access modifier here.

get {return 0;}

}

}

Implementing Interfaces

When you use an accessor to implement an interface, the accessor may not have an access modifier. However, if you implement the interface using one accessor, such as get, the other accessor can have an access modifier, as in the following example:

public interface ISomeInterface

{

int TestProperty

{

// No access modifier allowed here

// because this is an interface.

get;

}

}

public class TestClass : ISomeInterface

{

public int TestProperty

{

// Cannot use access modifier here because

// this is an interface implementation.

get { return 10; }

// Interface property does not have set accessor,

// so access modifier is allowed.

protected set { }

}

}