How to call a basic overloaded method in C #?

I have the following class hierarchy

class A { public virtual string M() { return M(String.Empty); } public virtual string M(string s) { return M(s, false); } public virtual string M(string s, bool flag) { // Some base logic here } } class B:A { public override string M(string s, bool flag) { string baseResult = base.M(s); // Derived class logic here } } 

Class B can be used in two cases:

1)

 A b = new B(); string result = bM(); 

2)

 B b2 = new B(); string result2 = b2.M(someString, true); 

Both cases are thrown using a StackOverflowException. This is because base.M (s), which is called inside BM (string s, bool flag), will again call BM (string s, bool flag).

Is there a good way to avoid this?

I understand that if I call base.M (s, flag), everything will work, but what if someone else develops the dervived class and gets access to the .M (s) base? I don't like to leave a StackOverflowException here.

Decision

Now my hierarchy will look like

 class A { public string M() { return M(String.Empty, false); } public virtual string M(string s, bool flag) { // Some base logic here } } class B:A { public override string M(string s, bool flag) { string baseResult = base.M(s, flag); // Derived class logic here } } 
+4
source share
3 answers

Usually the trick is to have one virtual (usually the one with most of the parameters), and this is the only one you call vertically. Others may not be virtual and just call the "main" with the appropriate default values.

+6
source

I would say something like this:

 class A { public virtual string M(string s = "", bool flag = false) { // Some base logic here } } 

instead of three overloaded methods that end up calling the same method with hard-coded parameters.

+2
source

You should not do this, but sometimes, when you need a cheap "hacker" solution, you can do the following:

 public interface IFooBar { void DoSomething(Object obj); } public class Foo { public virtual void DoSomething(Object input) { this.DoSomething(input, false); } protected virtual void DoSomething(Object input, bool skipSomeBits) { //Does stuff for Foo and Bar if (!skipSomeBits) { //Does stuff that is specific to Foo but does not need to happen to Bar } } } public class Bar : Foo { public override void DoSomething(object input) { base.DoSomething(input, true); } } 

Or (this is more suitable than indicated above) you can create a virtual method that is empty for child ( Bar ) and does not call base , but for the parent ( Foo ) it does the following:

 public interface IFooBar { void DoSomething(Object obj); } public class Foo { public virtual void DoSomething(Object input) { //Does Foo and Bar stuff this.DoSomething2(input); } protected virtual void DoSomething2(Object input) { //Does Foo stuff } } public class Bar : Foo { protected override void DoSomething2(Object input) { //Does not call base.DoSomething2() therefore does nothing or can do Bar stuff if needs be... } } 
0
source

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


All Articles