Recursive Function Calls Throw StackOverFlowException

后端 未结 1 1996
灰色年华
灰色年华 2020-12-12 00:11

I\'ve to call a function recursively. But after a moment it throws StackOverFlowException. When I used Invoke(new Action(Start)) method, it throws same exception but not in

相关标签:
1条回答
  • 2020-12-12 00:58

    When the last statement in a function is the call to the function itself, you have tail-recursion. While there are languages that optimize tail-recursion to avoid a stack overflow exception, C# is not one of them.

    Recursion is not a good pattern for data that can be of an arbitrary length. Simply replace recursion by a while loop:

    private void Start()
    {
        while(true) {
            // run select query
            mysql(selectQueryString.ToString());
            msdr = mysql();
            // is finished
            if (!msdr.HasRows)
            {
                this.Finish();
                break;
            }
            //  rest of your code..
        }
    }
    
    0 讨论(0)
提交回复
热议问题