5

I started out programming with C# a few days ago.

Now an confusing error arised when playing around with operator overloading.

The following code produces a StackOverflowException when running:

using System;

namespace OperatorOverloading
{
    public class Operators
    {
        // Properties
        public string text
        {
            get
            {
                return text;
            }

            set
            {
                if(value != null)
                    text = value;
                else
                    text = "";
            }
        }

        // Constructors
        public Operators() : this("")
        {
        }

        public Operators(string text)
        {
            // Use "set" property.
            this.text = text;
        }

        // Methods
        public override string ToString()
        {
            return text;
        }

        // Operator Overloading
        public static string operator +(Operators lhs, Operators rhs)
        {
            // Uses properties of the passed arguments.
            return lhs.text + rhs.text;
        }

        public static void Main(string[] args)
        {
            Operators o1 = new Operators();
            Operators o2 = new Operators("a");
            Operators o3 = new Operators("b");

            Console.WriteLine("o1: " + o1);
            Console.WriteLine("o2: " + o2);
            Console.WriteLine("o3: " + o3);

            Console.WriteLine();

            Console.WriteLine("o1 + o2: " + (o1 + o2));
            Console.WriteLine("o2 + o3: " + (o2 + o3));
        }
    }
}

I tried to write an own example after reading the chapter about operater overloading from the book "Microsoft Visual C# 2008" from Dirk Louis and Shinja Strasser.

Maybe someone has a clue what's going wrong.

Thanks.

2
  • your Main is inside the Operators class, is that something you wanted to do on purpose? Commented Feb 27, 2011 at 17:28
  • Just curious... why the accepted answer change from mine to curious geek's? The general rules are to mark the first answer that provided a working solution as accepted. Commented Feb 27, 2011 at 18:01

3 Answers 3

10

Well, for one, the operator overloading isn't breaking your code. You get a StackOverflowException because your text property's getter is trying to return itself.

You should use a backing field for your property:

private string _text;

public string Text
{
    get { return _text; }
    set
    {
        if (value != null)
            _text = value;
        else
            _text = string.Empty;
    }
}

What .NET does under the covers is convert your property into an accessor and a mutator -- two separate methods. In your original example, your code would be doing the following:

private string text;

public string get_text()
{
    return get_text(); // <-- StackOverflowException
}

public void set_text(string value)
{
    this.text = value;
}

Whereas the corrected version uses the backing field properly:

private string text;

public string get_text()
{
    return this.text; // Happy :)
}

public void set_text(string value)
{
    this.text = value;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I see plenty of operator overloading. However, you are correct in your answer.
@Mike - Yes, I didn't scroll down far enough. Whoopsy!
2

The get code-block in your class has the problem and that is what is causing the StackOverFlow exception.

    public string text
    {
        get
        {
            return text;
        }
    }

Here when you say return text; it will go and call the get block of the property text itself which is causing the stack-overflow. wrap your property text around a private _txt string field and it should work properly.

You can make it something like this..

private string _txt;
public string text
{
    get
    {
        return _txt;
    }

    set
    {
        _txt = string.IsNullOrEmpty(value) ? string.Empty : value;
    }
}

Comments

1

The problem is that the text proprty returns itself You should have a protected or private variab le to store the resut:

// Properties
    private string _text
    public string text
    {
        get
        {
            return _text;
        }

        set
        {
            if(value != null)
                _text = value;
            else
                _text = "";
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.