I work in .net 3.5. I have a class "A" that has a stack and a getter property that, when called, removes the first element on the stack and retrieves the next.
After initializing the class, I saw that the getter works without a call and removes the top element on the stack, which gives me bad results. The breakpoint at the getter did not show anyone passing through it.
When I change a property to a function, the stack returns normally.
I would be glad if someone could explain why.
Here is a 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(); }; } }
source share