0

What is the purpose of virtual in C#? What is the proper way and reason to use it?

For example, public virtual ICollection<Order> Orders { get; set; } in defining a collection orders in a Client class (say, at a restaurant or something). Why would you use virtual here? What does it do, and what is the general correct way to use it?

muttley91
  • 12,278
  • 33
  • 106
  • 160
  • Check this link http://msdn.microsoft.com/en-us/library/9fkccyh4.aspx – Esteban Elverdin Sep 26 '13 at 00:21
  • 1
    It is one of the three pillars on which Object Oriented Programming rests. Too large a subject to handle in an SO answer, it is well covered in many books you'll find in your local bookstore or library. – Hans Passant Sep 26 '13 at 00:32

3 Answers3

1

MSDN has the answer http://msdn.microsoft.com/en-us/library/9fkccyh4.aspx

The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it:

If you were to create a child class that inherits from the Client, you would be able to override this method and make it read only, or modify the get clause, limit the size of the collection added.....etc

ProFishChris
  • 55
  • 1
  • 8
1

Defining a property/method/etc of a class as virtual gives you the ability to override it in a derived class.

Why you would use it with a 'client class say at a restaurant or something' depends on how you differentiate between different clients. If one subgroup of clients always orders the same thing, you could for example override the Orders property in their derived class (from client) to always return that particular order.

Adam
  • 533
  • 4
  • 12
0

Virtual functions are not specific to C#. They allow algorithms, implemented to operate on objects of certain base class Parent, to continue functioning on objects of class Child, derived from class Parent, without knowing Child's implementation details.

A simple example: Object.ToString. We may have a form with various controls. If we want to check what controls we have, we could do:

foreach (control in form.Controls)
  Debug.Print(control.ToString())

Each control belongs to a certain class which always has Object class in its ancestry tree, and can override ToString to provide its own meaningful implementation of it. However, we don't need to know the implementation details of ToString of each control class we use on the form, to make this work. We don't even need to know if ToString has been overridden or not.

Sometimes, designers of class libraries deliberately mark a class method as virtual, even though the default method implementation is expected to be quite generic at the first glance. That's done for future flexibility. This might be the case with your public virtual ICollection<Order> Orders { get; set; } example. A developer who may be deriving a new class from the Client class in the future will have an option to provide an alternative behavior for Orders property. The code in the base library which, for example, is responsible for processing orders, will continue to work correctly with such new classes, on the binary level. There will be no need to recompile the base library.

noseratio
  • 59,932
  • 34
  • 208
  • 486