Mackintoast Mackintoast - 3 months ago 20
C# Question

virtual properties

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<B> prop{get;set;}
}


Could someone tell me what that means ?

Answer
public virtual ICollection<B> Prop { get; set; }

Translates almost directly to:

private ICollection<B> _Prop;

public virtual ICollection<B> get_Prop()
{
    return _Prop;
}

public virtual void set_Prop(ICollection<B> value)
{
    _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<B> Prop
{
    get { return null; }
    set { }
}
Comments