virtual properties

前端 未结 7 1431
迷失自我
迷失自我 2020-12-14 00:00

I have used and learned only virtual methods of the base class without any knowledge of virtual properties used as

class A
{
   public virtual ICollection<         


        
7条回答
  •  天命终不由人
    2020-12-14 00:37

    public virtual ICollection Prop { get; set; }
    

    Translates almost directly to:

    private ICollection m_Prop;
    
    public virtual ICollection get_Prop()
    {
        return m_Prop;
    }
    
    public virtual void set_Prop(ICollection value)
    {
        m_Prop = value;
    }
    

    Thus, the virtual keyword allows you to override the property in sub-classes just as you would the above get/set methods:

    public override ICollection Prop
    {
        get { return null; }
        set { }
    }
    

提交回复
热议问题