I have a simple if statement:
public void AMethod()
{
//AMethod works and does its thing
if (something == some value)
{
AMethod();
}
}
something is equal to some value very rarely and after running AMethod() stops being equal to some value.
AMethod() is also called rarely.
My questions are :
what are the disadvantages of doing so?
what are the alternatives if a method needs to be re-called from within the same method?
Thank you for your answers!
Having a method call itself is fine, as long as you’re careful to ensure it can’t get caught in an infinite loop. Remember that anything in your function that’s after the self-call won’t happen until the self-call is completely finished (but will still happen, if you don’t do anything to stop it).
See also: Recursion
5 Likes
This is called recursion, as Anti points out above.
To understand recursion, you must first understand recursion.
4 Likes
Antistone:
Having a method call itself is fine, as long as you’re careful to ensure it can’t get caught in an infinite loop. Remember that anything in your function that’s after the self-call won’t happen until the self-call is completely finished (but will still happen, if you don’t do anything to stop it).
See also: Recursion
Thank you for your response, Antistone! Yes, that’s the word i was looking for, thank you!
Kurt-Dekker:
This is called recursion, as Anti points out above.
To understand recursion, you must first understand recursion.
Thank you for your response, Kurt-Dekker! To understand anything you must first understand understanding :).
1 Like