C# setter 中的堆栈溢出异常
这有效:
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");
}
}
}
这不起作用:
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");
}
}
}
我在第二类中的 a 的 setter 上遇到堆栈溢出异常,我不知道为什么。我无法使用第一种形式,因为 Unity 游戏引擎 不支持它。
This works:
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");
}
}
}
This does not:
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");
}
}
}
I get a stack overflow exception on a's setter in the second class and I do not know why. I cannot use the first form because it is not supported by the Unity game engine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您编写
a = value
时,您将再次调用属性设置器。为了使用非自动属性,您需要创建一个单独的私有支持字段,如下所示:
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:
您还没有声明支持变量 - 您只是获得了一个属性,其 getter 和 setter 调用自身。我不清楚为什么 Unity 不支持第一种形式 - 这意味着等效形式也可能不受支持,但基本上是这样的:
我通常有一个更传统的形式当然,名称 - 这意味着您可以在没有“值”位的情况下逃脱:
为了更详细地说明为什么当前的第二种形式抛出 StackOverflowException ,您应该始终记住属性是基本上是伪装的方法。你损坏的代码看起来像这样:
希望修改后的版本只是设置一个变量而不是再次调用该属性,所以它看起来像这样:
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:
I'd normally have a more conventional name, of course - which means you can get away without the "value` bit:
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: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:
不能在 getter 和 setter 中使用相同的变量名。这将导致它调用自身并最终导致堆栈溢出。太多的递归。
您需要一个支持变量:
You cannot use the same variable name inside the getter and setter. This will cause it to call itself and will eventually lead to a stack overflow. Too much recursion.
You'll need a backing variable:
您的公共财产中需要一个私有支持变量:
You need a private backing variable in your public property: