Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

system.stackoverflowexception Cannot evaluate expression because the current thread is in a stack overflow state

I am getting a System.StackOverFlowException when the code hits this function.

Where stringtype is user defined tupe and equals int the function in the type library.

  public static bool Equals(StringType leftHand, StringType rightHand)
  {
       if (leftHand == rightHand)
       {
          return true;
       }
       if ((leftHand == "0") || (rightHand == "0"))
       {
          return false;
       }
       return (leftHand.myValue.Equals(rightHand.myValue) && leftHand.myState.Equals(rightHand.myState));
   }
like image 389
Pinu Avatar asked May 06 '26 04:05

Pinu


1 Answers

This

if (leftHand == rightHand)

change to

if (object.ReferenceEquals(leftHand, rightHand))

You probably redefined the == operator to call Equals.

And I hope you don't have an implicit operator that from string creates StringType, because otherwise

if ((leftHand == "0") || (rightHand == "0"))

will probably call itself for the same reason.

Probably

if ((leftHand.myValue == "0") || (rightHand.myValue == "0"))

would be better.

like image 50
xanatos Avatar answered May 08 '26 17:05

xanatos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!