Вопрос

Is there a way to hide a member of the base-class?

class A
{
  public int MyProperty { get; set; }
}

class B : A
{
  private new int MyProperty { get; set; }
}

class C : B
{
  public C()
  {
    //this should be an error
    this.MyProperty = 5;
  }
}
Это было полезно?

Решение

There is not a means to hide members in the C# language. The closest you can get is to hide the member from the editor, using EditorBrowsableAttribute.

public class B : A
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    new public int MyProperty {
        get;
        set;
    }
}

I would dare say that there is no guaruntee that this will work for other editors than Visual Studio, so you are better off throwing an exception on top of it.

public class B : A
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    public new int MyProperty {
        get {
            throw new System.NotSupportedException();
        }
        set {
            throw new System.NotSupportedException();
        }
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top