Overriding C # Function

I was just trying to master the concept of a virtual function using a console application. I noticed, as soon as I redefine the function of the base class, returns baseclassname.functionname (parameters) is automatically added to my function body. Why is this happening?

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); } } 
+5
source share
7 answers

By overriding a virtual function, you extend the functionality of your base class, and then call the base class for "base functionality."

If you delete the line ' return base.calc(a,b) ', the base class code will not be executed.

If you want to completely replace functionality, this is not a problem, but if you want to extend functionality, you must 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(); } 
+9
source

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

Sometimes overridden functions extend the functionality of base class functions. In this case, you can call base.function somewhere inside the overridden.

+4
source

This means that when it calls base.calc, it calls the parent function, which is overriding. If you do not want to call the basic function, you can delete it, but in most cases it will remain (that is why the visual studio automatically creates it).

If you do not want to call the a * b function in your extended function, you must remove the call to the base function and write your own.

eg. if you declare this in your advn class:

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

When you call this function, you will get the following:

 advanc myObj = new advanc(); advn advnObj = new advn(); myObj.calc(5, 2); // will return 10 advnObj.calc(5, 2); // will return 25 
+2
source

The IDE is trying to generate the simplest possible overridden method that actually compiles (so if you leave the actual implementation for a later version, you still have a piece of code that doesn't do much, but still compiles and produces a result that, probably not breaking the rest of your code).

In addition, there are some scenarios when calling a base class method makes sense even after your implementation:

  • the behavior of your child class expands on the basis, but does not completely replace it.
  • you use the decorator template , but your calc method does not need to be changed (you still need to implement it, because it is partly the original interface that you decorate, but you just redirect the call to your base class without changing the behavior)
+2
source

The IDE simply inserts a call to the underlying implementation. This means that, by default, the override behavior will be identical to how it overrides the method. If this is not the desired behavior (most likely not), just cancel the code of your choice.

+1
source

By default, VS suggests at least doing what the parent does in the function. In addition, in many cases, you need to start the parent and then perform your action (for example, in cases of the user interface). for base see MSDN about this:

The base keyword is used to access members of a base class from in 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. 

Access to the base class is allowed only in the constructor, instance, method or property of access to the instance.

+1
source

The body of the main function is automatically called due to the string return base.calc(a, b); But you can just make the body of the main function not start automatically and call your owen like this:

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

Source: https://habr.com/ru/post/1383608/


All Articles