how to fix Exception of type 'System.StackOverflowException' was thrown.' in c#

Dineshkumar.S 446 Reputation points
2023-03-06T07:25:21.0033333+00:00

In my C# windows application when i try to execute a method it throws me the Exception of type 'System.StackOverflowException' was thrown.' exception how to fix it and i am getting the infinite loop at the below code :

 public bool Compare(string answerFile, string correctFile, bool cellDiffFlag, 
                                 out bool isCorrect, out string strOut)
        {
            return Compare(answerFile, correctFile, cellDiffFlag, out isCorrect, out strOut);
        }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,813 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,323 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,159 questions
{count} votes

Accepted answer
  1. Michael Taylor 47,461 Reputation points
    2023-03-06T15:49:19.7366667+00:00

    That is because you are recursively calling yourself. Your Compare method calls itself which calls itself which calls itself... At some point you run out of stack space. Inside your Compare method you surely don't want to call yourself. Replace that call with the code you actually want to call. At some point somebody has to have the logic to do the actual comparison and that is what goes into this Compare method.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful