Overloading == stack overflow exception

Posted on 2007-08-09
Medium Priority
Last Modified: 2013-12-17
I'm trying to overload the == and != operators.

 public static bool operator ==(VeryLargeInt value1, VeryLargeInt value2)
            if ((value1 == null) || (value2 == null))
                throw new ArgumentNullException();
            return value1.Equals(value2);

        public static bool operator !=(VeryLargeInt value1, VeryLargeInt value2)
            return !(value1 == value2);

But the code above generates a unhandled exception of 'System.StackOverflowException'. How do I remedy this?
Question by:igor92128
LVL 37

Accepted Solution

gregoryyoung earned 2000 total points
ID: 19666416
your overload is on VeryLargeInt I take it?

It just calls == itself ... when it calls == this just calls back into itself again (infinite recursion). should it be doing some other comparison here (like actually doing a comparison? or maybe calling .Equals?).
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 19667863
just make the == like this:

 public static bool operator ==(VeryLargeInt value1, VeryLargeInt value2)
            return value1.Equals(value2);

 if eiter value is null, that will raise an error anyhow...

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month16 days, 18 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question