overriding a function c#
Asked Answered
S

7

7

I was just trying to master the concept of virtual function using a console app. I noticed as soon as I override a base class function, return baseclassname.functionname(parameters) gets inserted in my function body automatically. Why does this happen?

class advanc
{
    public virtual int  calc (int a , int b)
    {        
         return (a * b);        
    }        
}

class advn : advanc
{
     public override int calc(int a, int b)
     {
          //automatically inserted
          return base.calc(a, b);
      }        
}
Spermiogenesis answered 29/11, 2011 at 10:42 Comment(0)
O
12

By overriding a virtual function you expand the functionality of your base class and then call the base class for the 'base functionality'.

If you would remove the 'return base.calc(a,b)' line, the base class code would not execute.

If you want to completely replace the functionality this is not a problem but if you want to extend the functionality then you should also call the base class.

The following code demonstrates this (just put it in a Console Application)

class advanc
{
    public virtual int calc(int a, int b)
    {
        Console.WriteLine("Base function called");
        return (a * b);
    }
}

class advn : advanc
{
    public bool CallBase { get; set; }
    public override int calc(int a, int b)
    {
        Console.WriteLine("Override function called");

        if (CallBase)
        {
            return base.calc(a, b);
        }
        else
        {
            return a / b;
        }
    }
}

private static void Main()
{
    advn a = new advn();

    a.CallBase = true;
    int result = a.calc(10, 2);
    Console.WriteLine(result);

    a.CallBase = false;
    result = a.calc(10, 2);
    Console.WriteLine(result);
    Console.WriteLine("Ready");
    Console.ReadKey();
}
Olshausen answered 29/11, 2011 at 10:46 Comment(3)
can u please explain what is get and set??i did not find very good example reg that..is it a way to intiallize variablesSpermiogenesis
{get; set;} is an auto implemented property. A property uses get and set to encapsulate a field so it can't be read or modified directly. This is a good programming practice. You can check msdn.microsoft.com/en-us/library/bb384054.aspx if you want to read moreOlshausen
The statement "If you would remove the 'return base.calc(a,b)' line, the base class code would not execute." is just complete nonsense. Yes, you can remove that line and write whatever you want to override the base virtual function. This has nothing to do with the base class code.Splat
A
5

Visual Studio is trying to guess what you want, so it suggests you this code.

Sometimes, the overridden functions expand the functionality of the base class functions. In this case it is viable to call base.function somewhere inside the overridden one.

Albanian answered 29/11, 2011 at 10:45 Comment(0)
G
2

It means that when it calls base.calc it will call the parents function that it is overriding. If you don't want to call the base function you can remove it but in most cases it will remain (which is why visual studio auto generates it).

If in your advanced function you didn't want to call the a * b functionality you would remove the base function call and write your own.

E.G. if you declare this in your advn class :

public override int calc(int a, int b)
{
   return Math.Pow(a, b);
}

You will get the following when calling this functionality :

advanc myObj = new advanc();
advn advnObj = new advn();

myObj.calc(5, 2); // will return 10
advnObj.calc(5, 2); // will return 25
Gwinn answered 29/11, 2011 at 10:47 Comment(1)
I would have finished my edit earlier but I heard the food van pull up outside!Gwinn
A
2

the IDE is trying to generate the easiest possible overridden method that actually compiles (so if you leave the actual implementation for later, you still have a piece of code that does not do much but still compiles and yields a result that is probably not going to break the rest of your code).

Also, there are some scenarios where the call to the base class's method makes sense even after your implementation:

  1. the behaviour of your child class expands on the base but does not replace it completely
  2. you're using the decorator pattern but your calc method doesn't need to be changed (you still need to implement it because it's part of the original interface you're decorating, but you are just forwarding the call to your base class without changing the behavior)
Aguedaaguero answered 29/11, 2011 at 10:55 Comment(0)
S
1

The IDE is simply inserting a call to the base implementation. This means that, by default, the behaviour of the override will be identical to the method it overrides. If this is not the desired behaviour (likely not), simply repace with the code of your choice.

Schooling answered 29/11, 2011 at 10:45 Comment(0)
S
1

By default VS offers to at least do what the parent does in the function. In addition, in many cases you need to run parent then do your action (e.g. in UI cases). for base see MSDN about it:

The base keyword is used to access members of the base class from within a derived class:

Call a method on the base class that has been overridden by another method.
Specify which base-class constructor should be called when creating instances 
of the derived class.

A base class access is permitted only in a constructor, an instance method, or an instance property accessor.

Substitution answered 29/11, 2011 at 10:46 Comment(0)
F
1

The base function body gets invoked automatically becouse of the line return base.calc(a, b); But, you can simply make the base function body doesn't gets invoked automatically, and invoked your owen like this:

    class advn : advanc
    {
        public override int calc(int a, int b)
        {
             return a * b; //anything else;
        }

    }
Felicitation answered 29/11, 2011 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.