0

I have seen people use the virtual keyword for property like this:

public class Client
{
    public virtual int? Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Lastname { get; set; }
    public virtual int? Age { get; set; }
    public virtual ISet<Login> Logins { get; set; }
} 

All we know when we declare anything as virtual is that in a derived class we can override it, but could there also be another reason for using this keyword on a property?.

If you could explain it with an example it might clear things up.

Aage
  • 5,932
  • 2
  • 32
  • 57
Mou
  • 15,673
  • 43
  • 156
  • 275
  • That looks like NHibernate's POCO class. NHibernate builds a proxy classes on top of those POCOs to facilitate lazy loading, so it requires `virtual` properties... – Patryk Ćwiek Nov 05 '13 at 07:02

2 Answers2

3

They use the virtual keyword for exactly this reason: allow derived classes to override behaviour. Properties have behaviour that can be overriden, too. Just because this base class uses auto-properties does not mean all derived classes have to.

public class IdCheckingClient : Client
{
    private int? backingfieldId;

    public override int? Id
    {
        get { return backingfieldId; }
        set 
        {
           if(value.HasValue && value.Value < 0)
           {
               throw new ArgumentException("ID needs to be positive or null");
           }
           backingfieldId = value;
        }  
    }
} 
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • i know the use virtual keyword & override relatiob but i like to know how virtual keyword help to implement lazy loading. guide me in details if possible. thanks – Mou Nov 05 '13 at 07:58
2

Just like Patryk says, it's for being able to work smoothly with an ORM like NHibernate or Entity Framework. This ORM then builds proxy classes on top of those properties declared as virtual to enable lazy loading on them (getting all the navigation data).

Example:

public class User
{
   public int ID { get; set; }
   public virtual Role Role { get; set; }
}
public class Role
{
   public int ID { get; set; }
   public string Name { get; set; }
   public virtual List<User> UsersInRole { get; set; }
} 

When you get a user you will automatically get the associated Role. If you get a Role you automatically get all the Users in that Role.

Docs of Entity Framework: http://msdn.microsoft.com/en-us/data/jj679962

Gecko
  • 1,333
  • 1
  • 14
  • 26
  • u said:- This ORM then builds proxy classes on top of those properties declared as virtual to enable lazy loading on them. can u plzz redirect me to few article which show how virtual keyword help to implement lazy loading. thanks – Mou Nov 05 '13 at 07:56
  • Depends on which ORM you would like to use. Personally I prefer Entity Framework. http://msdn.microsoft.com/en-us/data/ee712907#codefirst here is everuthing basic to start with EF. I always do the Code First to have the utmost control over my relations. https://github.com/stefchri/ArticulatieOnderzoek this is a project with this implemented. – Gecko Nov 05 '13 at 10:18
  • I would like to know if you would use virtual keyword here if you were not using ADO.NET instead of any ORM – SamuraiJack Oct 08 '19 at 11:24
  • @SamuraiJack depends if you are going to make derived classes and override properties. If not, then no. – Gecko Oct 09 '19 at 04:11