Operator overload causes stack overflow

I started programming with C # a few days ago.

Now a confusing error occurs when playing with operator overload.

The following code throws a StackOverflowException :

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 my own example after reading the chapter on operator overloading from the book "Microsoft Visual C # 2008" by Dirk Louis and Shinji Strasser.

Perhaps someone knows what is going wrong.

Thank.

+5
source share
3 answers

The get code in your class has a problem, and that is what causes the StackOverFlow exception.

    public string text
    {
        get
        {
            return text;
        }
    }

, return text;, get text, . _txt .

.

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

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

, -, . StackOverflowException, get text .

:

private string _text;

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

.NET , - . :

private string text;

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

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

:

private string text;

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

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

, varab :

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

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

All Articles