Question

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;
  }
}
Was it helpful?

Solution

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();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top