我在.net 3.5中工作。 我有一个类“A”,它有一个堆栈和一个getter属性,当被调用时,它将删除堆栈中的第一个项目并检索下一个项目。
在初始化这个类之后,我看到getter工作时没有被调用,并且删除了堆栈中的顶层项目,从而给我带来不好的结果。 吸气剂中的断点没有显示任何人通过它。
当我将属性更改为函数时,堆栈返回正常。
如果有人能解释为什么,我会很高兴。
这是简化的课程:
public class A { private Stack<string> Urls; public A(string title, string[] array) { Urls = new Stack<string>(); foreach (string s in array) { Urls.Push(s); } } public string Url { get { return Urls.Peek(); } } public string NextUrl { get{ if (Urls.Count > 1) { Urls.Pop(); } return Urls.Peek(); }; } }I'm working in .net 3.5. I have a class "A" which has a stack and a getter property which, when called, removes the first item in the stack and retrieves the next one.
After initializing the class, I saw that the getter works without being called, and removes the top item in the stack, thus giving me bad results. A breakpoint in the getter did not show anyone passing through it.
When I change the property to a function, the stack is returned ok.
I'd be happy if someone could explain why is that.
Here is the simplified class:
public class A { private Stack<string> Urls; public A(string title, string[] array) { Urls = new Stack<string>(); foreach (string s in array) { Urls.Push(s); } } public string Url { get { return Urls.Peek(); } } public string NextUrl { get{ if (Urls.Count > 1) { Urls.Pop(); } return Urls.Peek(); }; } }最满意答案
首先,让一个属性访问器改变状态通常是一个坏主意。 最应该做的是懒惰地初始化一些东西 - 或者可能给一个不稳定的值(比如DateTime.Now )。
其次,如果你在调试器下运行,你可能会看到这一点 - 当你逐步完成代码时,它会访问属性。 这也许可以解释为什么断点也没有被击中。
Firstly, making a property accessor change the state is generally a bad idea. The most it should do is lazily initialize something - or possibly give a volatile value (like DateTime.Now does).
Secondly, you're probably seeing this if you're running under the debugger - it accesses properties while you're stepping through code. That would probably explain why the breakpoint wasn't being hit, too.
Getter属性运行时没有任何人调用它(Getter property is run without anyone calling it)我在.net 3.5中工作。 我有一个类“A”,它有一个堆栈和一个getter属性,当被调用时,它将删除堆栈中的第一个项目并检索下一个项目。
在初始化这个类之后,我看到getter工作时没有被调用,并且删除了堆栈中的顶层项目,从而给我带来不好的结果。 吸气剂中的断点没有显示任何人通过它。
当我将属性更改为函数时,堆栈返回正常。
如果有人能解释为什么,我会很高兴。
这是简化的课程:
public class A { private Stack<string> Urls; public A(string title, string[] array) { Urls = new Stack<string>(); foreach (string s in array) { Urls.Push(s); } } public string Url { get { return Urls.Peek(); } } public string NextUrl { get{ if (Urls.Count > 1) { Urls.Pop(); } return Urls.Peek(); }; } }I'm working in .net 3.5. I have a class "A" which has a stack and a getter property which, when called, removes the first item in the stack and retrieves the next one.
After initializing the class, I saw that the getter works without being called, and removes the top item in the stack, thus giving me bad results. A breakpoint in the getter did not show anyone passing through it.
When I change the property to a function, the stack is returned ok.
I'd be happy if someone could explain why is that.
Here is the simplified class:
public class A { private Stack<string> Urls; public A(string title, string[] array) { Urls = new Stack<string>(); foreach (string s in array) { Urls.Push(s); } } public string Url { get { return Urls.Peek(); } } public string NextUrl { get{ if (Urls.Count > 1) { Urls.Pop(); } return Urls.Peek(); }; } }最满意答案
首先,让一个属性访问器改变状态通常是一个坏主意。 最应该做的是懒惰地初始化一些东西 - 或者可能给一个不稳定的值(比如DateTime.Now )。
其次,如果你在调试器下运行,你可能会看到这一点 - 当你逐步完成代码时,它会访问属性。 这也许可以解释为什么断点也没有被击中。
Firstly, making a property accessor change the state is generally a bad idea. The most it should do is lazily initialize something - or possibly give a volatile value (like DateTime.Now does).
Secondly, you're probably seeing this if you're running under the debugger - it accesses properties while you're stepping through code. That would probably explain why the breakpoint wasn't being hit, too.
发布评论