Stack overflow exception in c# setter

孤街浪徒 提交于 2019-11-28 13:20:13

When you write a = value, you are calling the property setter again.

In order to use non-automatic properties, you need to create a separate private backing field, like this:

ConstraintSet a;
public ConstraintSet A { get { return a; } set { a = value; } }

You haven't declared a backing variable - you've just got a property whose getters and setters call themselves. It's not clear to me why the first form isn't supported by Unity - which means it's possible that the equivalent won't be supported either, but it's basically this:

private ConstraintSet aValue;
public ConstraintSet a { get { return aValue; } set { aValue = value; } }

I'd normally have a more conventional name, of course - which means you can get away without the "value` bit:

private ConstraintSet constraints;
public ConstraintSet Constraints
{
    get { return constraints; } 
    set { constraints = value; }
}

To give a bit more detail as to why your current second form is throwing a StackOverflowException, you should always remember that properties are basically methods in disguise. Your broken code looks like this:

public ConstraintSet get_a()
{
    return get_a();
}

public void set_a(ConstraintSet value)
{
    set_a(value);
}

Hopefully it's obvious why that version is blowing the stack. The amended version just sets a variable instead of calling the property again, so it looks like this when expanded:

private ConstraintSet aValue;

public ConstraintSet get_a()
{
    return aValue;
}

public void set_a(ConstraintSet value)
{
    aValue = value;
}

You cannot use the same variable name inside the getter and setter.
This will cause it to call itself and will eventually stackoverflow. Too much recursion.
You'll need a backing variable:

private ConstraintSet _a;
public ConstraintSet a { get { return _a; } set { _a = value; } }

You need a private backing variable in your public property:

private ConstraintSet _a;
public ConstraintSet a { get { return _a; } set { _a = value; } }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!