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

asp.net-mvc asp.net-mvc-3 c# entity-framework

Question

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

1
22
10/1/2012 7:29:22 PM

Accepted Answer

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.

12
10/1/2012 6:27:18 PM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow