I asked the question before , but I donβt think I was clear enough about the answers that I was hoping for, so let me provide a more specific example:
class Program { private static bool State; static void Main(string[] args) { State = false; Console.WriteLine(And()); Console.ReadLine(); } static bool And() { return Or() && C(); } static bool Or() { return A() || AB(); } static bool C() { return State; } static bool A() { return true; } static bool AB() { State = true; return true; } }
The flow of this program is as follows:
- And () is called
- And () calls Or ()
- Or () calls A ()
- A () returns true
- The thread returns to Or (), which returns true (lazy evaluation)
- The thread returns to calls to And (), And () C ()
- C () returns false
- The stream returns AND (), which returns false
Now, if Or () has not performed a lazy evaluation (changing || to | ), the program will return true. However, I do not want AB () to be executed if the result of the full analysis did not work (And () returns false).
So, what I would like to do is somewhere in the Or () function, save the current state on the stack (static variable), so that if And () returns false, I can pull an element from the stack and try an alternative.
How would this be done in C #?
source share