65

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 ?

Mackintoast
  • 1,397
  • 5
  • 17
  • 25

7 Answers7

80
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 { }
}
Zenexer
  • 18,788
  • 9
  • 71
  • 77
31

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", and not "behnoud", because the "Name" property has been overridden in the derived class(sub class).

joym8
  • 4,014
  • 3
  • 50
  • 93
16

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.

Huske
  • 9,186
  • 2
  • 36
  • 53
2

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.

Tarik
  • 79,711
  • 83
  • 236
  • 349
1

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Stephane Halimi
  • 408
  • 3
  • 5
1

Properties are a shortened form of accessor methods (Get & Set). That means that the virtual keyword has the same meaning as with any other method. That means you can override it in derived classes.

loodakrawa
  • 1,468
  • 14
  • 28
0

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

Chriseyre2000
  • 2,053
  • 1
  • 15
  • 28