dcsimg
Results 1 to 2 of 2

Thread: The essence of the virtual methods in C#

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2017
    Posts
    3

    The essence of the virtual methods in C#

    Dear experts,
    I would like to understand the ins and outs of the background of the dynamic binding. I would like to take advantage of the virtual methods, but I got stuck in the correct usage. I don't know what is difference in the examples between the virtual methods and the "simple" methods. I learn about the dynamic binding happens in them "run time". ( maybe it would be the difference ) Or could you show me good example which highlight the importance of the usage of virtual methods ?

    Thank you very much for you help in advance.

    here is my simplified code snippet (containing a virtual method or a "simple" method too, but it no difference between them. ). :

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Vehicles
    {
        class Vehicle {                      
            public virtual void NumberOfWheels()  // virtual method
            {
                Console.WriteLine("The number of wheels:");
            }
    
            public void Wheels() // not virtual method - What is the difference from the prev. declared virtual method ? 
            {
                Console.WriteLine("The number of wheels:");
            }
        }
        class Motor : Vehicle {               
            public override void NumberOfWheels() 
            {
                
                Console.WriteLine("Motor: 2");
            }
    
            public void Wheels()
            {
                Console.WriteLine("Wheels (not virtual): Motor: 2");
            }
        }
        class Auto : Vehicle
        {
            public void NumberOfWheels() // not override
            {
    
                Console.WriteLine("Auto: 4");
            }
    
            public void Wheels()
            {
                Console.WriteLine("Wheels (not virtual): Auto: 4");
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Motor instance: ");
                Motor myBabetta = new Motor();
                myBabetta.NumberOfWheels();
                myBabetta.Wheels();
                Console.WriteLine("Auto instance: ");
                Auto myFathersFiat = new Auto();
                myFathersFiat.NumberOfWheels(); // virtual method
                myFathersFiat.Wheels();   // not virtual method - but the result is the same
                Console.ReadKey();
            }        
        }
    
    }

  2. #2
    Fanatic Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    982

    Re: The essence of the virtual methods in C#

    Try changing your Main to be something like
    Code:
     Console.WriteLine("Motor instance: ");
                Vehicle myBabetta = new Motor();
                myBabetta.NumberOfWheels();
                myBabetta.Wheels();
                Console.WriteLine("Auto instance: ");
                Vehicle myFathersFiat = new Auto();
                myFathersFiat.NumberOfWheels(); // virtual method
                myFathersFiat.Wheels();   // not virtual method - but the result is the same
                Console.ReadKey();
    Notice how it allows you to work with a variable of type Vehicle, but still assign an Auto or a Motor. You get the correct version of the method called despite the variable having the Vehicle type.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width