Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - when to use public int virtual, and when to just use public int

I am working through a tutorial 'Professional ASP.NET MVC 3' by J Galloway. In this tutorial, Jon shows us how to build the MVC music store.

I am at the part where we are creating CS classes to model the data using EF code first.

I all the examples in the book, public virtual int property {get; set; } is used with no explanation. The term virtual is stuffed EVERYWHERE.

Elsewhere on the web, I have not seen the term virtual used with any kind of concistency whatsoever.

Could anybody explain to me:

  1. The purpose of the term 'virtual' in this particular context
  2. Is using 'virtual' necessary?
  3. Why do some people use 'virtual' and others do not?
  4. Why do some people only use 'virtual' when defining foreign keys?
  5. What is the best practice use of the term 'virtual'?

Many thanks in advance

like image 264
Gravy Avatar asked Oct 01 '12 18:10

Gravy


2 Answers

In order to truly understand the virtual keyword you are going to want to read up on Polymorphism in general:

Polymorphism is often referred to as the third pillar of object-oriented programming, after encapsulation and inheritance. Polymorphism is a Greek word that means "many-shaped" and it has two distinct aspects:

  1. At run time, objects of a derived class may be treated as objects of a base class in places such as method parameters and collections or arrays. When this occurs, the object's declared type is no longer identical to its run-time type.

  2. Base classes may define and implement virtual methods, and derived classes can override them, which means they provide their own definition and implementation. At run-time, when client code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. Thus in your source code you can call a method on a base class, and cause a derived class's version of the method to be executed.

Once you understand these concepts better you might be able to determine whether or not the method you are creating from the book needs to be virtual or not.

like image 131
Andrew Hare Avatar answered Oct 20 '22 13:10

Andrew Hare


Could anybody explain to me:

  1. The purpose of the term 'virtual' in this particular context

    Other users here answered this well with very good references.

  2. Is using 'virtual' necessary?

    It depends. Sometimes it is necessary, sometimes it is superfluous.

  3. Why do some people use 'virtual' and others do not?

    They use it when they need it, or when they think they might need it.

  4. Why do some people only use 'virtual' when defining foreign keys?

    When defining foreign keys for use by Object Relational Mapping tools like Entity Framework and NHibernate, virtual is often necessary because these ORM tools dynamically create a new class that inherits from your class. These "dynamic proxy" classes override your virtual properties to provide additional behavior needed to maintain foreign key consistency. In the case of NHibernate, all properties (not just foreign keys) must be marked virtual. This is because NH dynamic proxies add custom behavior to decide which properties on your model to retrieve from the database, and which to avoid loading.

  5. What is the best practice use of the term 'virtual'?

    Use it when you intend for a member (method or property) to be overridden in a more derived class. Do not use them in classes marked sealed.

like image 7
danludwig Avatar answered Oct 20 '22 15:10

danludwig