c#setter中的堆栈溢出异常

Pat*_*rik 14 c# stack-overflow setter callstack

这很容易解释:这很有效

using System;
using ConstraintSet = System.Collections.Generic.Dictionary<System.String, double>;

namespace ConsoleApplication2
{
    class test
    {
        public ConstraintSet a { get; set; }
        public test()
        {
            a = new ConstraintSet();
        }
        static void Main(string[] args)
        {
            test abc = new test();
            Console.WriteLine("done");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这不

using System;
using ConstraintSet = System.Collections.Generic.Dictionary<System.String, double>;

namespace ConsoleApplication2
{
    class test
    {
        public ConstraintSet a { get { return a; } set { a = value; } }
        public test()
        {
            a = new ConstraintSet();
        }
        static void Main(string[] args)
        {
            test abc = new test();
            Console.WriteLine("done");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在第二类的setter上得到了堆栈溢出异常,我不知道为什么.我不能使用第一种形式,因为统一引擎不支持它

SLa*_*aks 37

当你写作时a = value,你再次调用属性设置器.

要使用非自动属性,您需要创建一个单独的私有支持字段,如下所示:

ConstraintSet a;
public ConstraintSet A { get { return a; } set { a = value; } }
Run Code Online (Sandbox Code Playgroud)

  • 令人着迷的是,这个过程这么多年都没有改变。 (3认同)

Jon*_*eet 18

您尚未声明支持变量 - 您刚刚获得了一个属性,其getter和setter会自行调用.我不清楚为什么 Unity不支持第一种形式 - 这意味着它可能也不会支持等效,但它基本上是这样的:

private ConstraintSet aValue;
public ConstraintSet a { get { return aValue; } set { aValue = value; } }
Run Code Online (Sandbox Code Playgroud)

当然,我通常会有一个更传统的名字 - 这意味着你可以在没有"价值"位的情况下离开:

private ConstraintSet constraints;
public ConstraintSet Constraints
{
    get { return constraints; } 
    set { constraints = value; }
}
Run Code Online (Sandbox Code Playgroud)

为了更详细地说明你当前的第二种形式为什么抛出一个StackOverflowException,你应该永远记住属性基本上是伪装的方法.你破碎的代码看起来像这样:

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

public void set_a(ConstraintSet value)
{
    set_a(value);
}
Run Code Online (Sandbox Code Playgroud)

希望很明显为什么该版本正在吹嘘堆栈.修改后的版本只是设置一个变量而不是再次调用该属性,因此在展开时它看起来像这样:

private ConstraintSet aValue;

public ConstraintSet get_a()
{
    return aValue;
}

public void set_a(ConstraintSet value)
{
    aValue = value;
}
Run Code Online (Sandbox Code Playgroud)


San*_*nen 5

在 getter 和 setter 中不能使用相同的变量名。这将导致它调用自身并最终导致堆栈溢出。太多的递归。

你需要一个支持变量:

private ConstraintSet _a;
public ConstraintSet a { get { return _a; } set { _a = value; } }
Run Code Online (Sandbox Code Playgroud)