virtual properties

前端 未结 7 1419
迷失自我
迷失自我 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<B> Prop { get; set; }
    

    Translates almost directly to:

    private ICollection<B> m_Prop;
    
    public virtual ICollection<B> get_Prop()
    {
        return m_Prop;
    }
    
    public virtual void set_Prop(ICollection<B> 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<B> Prop
    {
        get { return null; }
        set { }
    }
    
    0 讨论(0)
  • 2020-12-14 00:37

    In object-oriented programming, a virtual property is a property whose behavior can be overridden within an inheriting class. This concept is an important part of the polymorphism portion of object-oriented programming (OOP).

    look at the example below:

    public class BaseClass
    {
    
        public int Id { get; set; }
        public virtual string Name { get; set; }
    
    }
    
    public class DerivedClass : BaseClass
    {
        public override string Name
        {
            get
            {
                return base.Name;
            }
    
            set
            {
                base.Name = "test";
            }
        }
    }
    

    at the presentation level:

            DerivedClass instance = new DerivedClass() { Id = 2, Name = "behnoud" };
    
            Console.WriteLine(instance.Name);
    
            Console.ReadKey();
    

    the output will be "test" because the "Name" property has been overridden in the derived class(sub class).

    0 讨论(0)
  • 2020-12-14 00:37

    In Entity Framework (which I believe your example refers to), your POCO classes are created and wrapped into a proxy class. Proxy class is a descendant of the class that you declare, so your class A becomes a base class. This proxy class is populated with data and returned back to you. This is necessary in order to track changes. Have a look at this article http://technet.microsoft.com/en-us/query/dd456848

    I had a similar problem in trying to understand this and after a few debugging sessions and seeing the proxy classes and reading about tracking changes it made be figure out why it is declared the way it is.

    0 讨论(0)
  • 2020-12-14 00:37

    Properties are actually specials cases of Getter and Setter methods. So they are like combinations of Getter and Setter methods as shown below:

    private string _name;
    
    public string GetName()
    {
       return _name;
    }
    
    public void SetName(string value)
    {
       this._name = value;
    }
    

    So virtual keyword is same for properties as well which means it is overrideable by the child classes and initial implementation can be changed.

    0 讨论(0)
  • 2020-12-14 00:37

    You can have methods (often), properties, indexers or events, the virtual keyword has the same meaning : modifying the meaning (override) of the base class item. With properties, you can change the get/set accessors.

    0 讨论(0)
  • 2020-12-14 00:44

    It's a collection that's implementation can vary in a descendant class.

    0 讨论(0)
提交回复
热议问题