2

I'm following a tutorial series about MVC4 on PluralSight and the presenter sets all of the properties of all of his models as virtual. I haven't seen others do this and I was wondering if it is the right way to create models. And further, what is the reason to use the virtual keyword.

for example:

public class Video
{
    public virtual int Id { get; set; }
    public virtual string Title { get; set; }
    public virtual int Length { get; set; }
}
Doug S.
  • 682
  • 1
  • 10
  • 26
  • 2
    possible duplicate of [Why use 'virtual' for properties in classes?](http://stackoverflow.com/questions/8542864/why-use-virtual-for-properties-in-classes) – Jeroen Vannevel Nov 01 '13 at 16:34
  • It's difficult to answer this question without the complete source code. I'm guessing they are using virtual properties because they plan to use the Entity Framework and this enable advanced features such as lazy loading and more efficient change tracking. See this questions for more details: http://stackoverflow.com/questions/8542864/why-use-virtual-for-properties-in-classes http://stackoverflow.com/questions/5597760/what-effects-can-the-virtual-keyword-have-in-entity-framework-4-1-poco-code-fi – Paolo Moretti Nov 01 '13 at 16:41

1 Answers1

1

It is not necessarily. In context of you tutorial, Virtual methods are used for Lazy Loading in Entities Framework.

P.S. About Lazy Load

Ilya Sulimanov
  • 7,636
  • 6
  • 47
  • 68