How can I hide a base class public property in the derived class

后端 未结 16 1375
自闭症患者
自闭症患者 2021-01-01 08:39

I want to hide the base public property(a data member) in my derived class:

class Program
{
    static void Main(string[] args)
    {
        b obj = new b()         


        
16条回答
  •  礼貌的吻别
    2021-01-01 09:12

    Changing the accessibility of a virtual member is an inheriting class is specifically prohibited by the C# language spec:

    The override declaration and the overridden base method have the same declared accessibility. In other words, an override declaration cannot change the accessibility of the virtual method. However, if the overridden base method is protected internal and it is declared in a different assembly than the assembly containing the override method then the override method’s declared accessibility must be protected.

    From section 10.6.4 Override methods

    The same rules which apply to overriding method also apply to properties, so going from public to private by inheriting from the base class can't be done in C#.

提交回复
热议问题