I installed a simple program to check how the code is executed inside the get accessor (since I had some problems in another project), and found something rather strange:
class Program { static void Main(string[] args) { var test = new TestClass(); var testBool = test.TestBool; } } public class TestClass { private bool _testBool = true; public bool TestBool { get { if (_testBool) { Console.WriteLine("true!"); } else { Console.WriteLine("false! WTF!"); } _testBool = false; return _testBool; } } }
I expected the output to be
right!
But instead I got
right!
Lying! WTF!
Just what is going on here?
source share