virtual properties

C#

C# Problem Overview


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 ?

C# Solutions


Solution 1 - C#

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 { }
}

Solution 2 - C#

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).

Solution 3 - C#

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.

Solution 4 - C#

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.

Solution 5 - C#

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.

Solution 6 - C#

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.

Solution 7 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMackintoastView Question on Stackoverflow
Solution 1 - C#ZenexerView Answer on Stackoverflow
Solution 2 - C#Behnoud SherafatiView Answer on Stackoverflow
Solution 3 - C#HuskeView Answer on Stackoverflow
Solution 4 - C#TarikView Answer on Stackoverflow
Solution 5 - C#loodakrawaView Answer on Stackoverflow
Solution 6 - C#Stephane HalimiView Answer on Stackoverflow
Solution 7 - C#Chriseyre2000View Answer on Stackoverflow