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

后端 未结 16 1377
自闭症患者
自闭症患者 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#.

    0 讨论(0)
  • 2021-01-01 09:15

    First of all this is not good idea if you using some methods, that operates base class.
    You can try to use obsolete argument to make users twice think to use this property.

    [System.Obsolete("Do not use this property",true)]  
    public override YourType YourProperty { get; set; }
    
    0 讨论(0)
  • 2021-01-01 09:16
    namespace PropertyTest    
    {    
        class a
        {    
            int nVal;
    
            public virtual int PropVal
            {
                get
                {
                    return nVal;
                }
                set
                {
                    nVal = value;
                }
            }
        }
    
        class b : a
        {
            public new int PropVal
            {
                get
                {
                    return base.PropVal;
                }
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                a objA = new a();
                objA.PropVal = 1;
    
                Console.WriteLine(objA.PropVal);
    
                b objB = new b();
                objB.PropVal = 10; // ERROR! Can't set PropVal using B class obj. 
                Console.Read();
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-01 09:20

    You cant do it directly, but you could override the properties in the child class and make them readonly e.g.

    class Program
    {
        static void Main(string[] args)
        {
            b obj = new b();
            obj.item1 = 4;// should show an error but it doent ???
        }
    }
    
    class a
    {
        public virtual int item1 {get; set;}
        public virtual int item2 { get; set; }
    
    }
    
    class b : a
    {
        public override int item1
        { 
             get { return base.item1; }
             set { }
        }
    }
    
        class c : a
    {
    
    }
    
    0 讨论(0)
提交回复
热议问题