In the beginning, there was code:
public abstract class A
{
private string _someValue;
public string SomeValue
{
get
{
if (_someValue == null)
_someValue = GetValue();
return _someValue;
}
}
protected virtual string GetValue() { /* logic */ }
}
public class B : A
{
protected override string GetValue()
{
return base.GetValue() + GetMoreValue();
}
private string GetMoreValue() { /* logic */ }
}
And the code said, "Let there be bugs!" and there were bugs.
Seriously now.
I have an instance of B
, and when I get SomeValue
, I get the same SomeValue
of A
, without the MoreValue.
The even weirder part came when I put a breakpoint on SomeValue
's Get method:
It turns out that _someValue
gets its value before the Get method is ever called.
Something is very wrong here.
UPDATE:
Thanks for the comments! Shortened code, and added forgotten return type in B method.