Overloading == stack overflow exception

Posted on 2007-08-09
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

    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 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now