Debugging a getter
-
Hi, I am using the following code for testing purposes:
public class VariousTests { public static void Main(string\[\] args) { AClass c = new AClass(); int y = c.DemandSupplyMatrix\[0, 0\]; // breakpoint 2 } } class AClass { private int\[,\] \_aMatrix = null; public int\[,\] DemandSupplyMatrix { get { if (\_aMatrix == null) \_aMatrix = new int\[9, 4\]; return \_aMatrix; // breakpoint 1 } } }
I run it until I hit breakpoint 2 and I expect to see
DemandSupplyMatrix
being null in the debugger. Instead, it appears to have been already instantiated, and I have never hit breakpoint 1... I really need to debug the getter but it seems that I have no chance. Is this normal or am I missing something?Bogdan.
-
Hi, I am using the following code for testing purposes:
public class VariousTests { public static void Main(string\[\] args) { AClass c = new AClass(); int y = c.DemandSupplyMatrix\[0, 0\]; // breakpoint 2 } } class AClass { private int\[,\] \_aMatrix = null; public int\[,\] DemandSupplyMatrix { get { if (\_aMatrix == null) \_aMatrix = new int\[9, 4\]; return \_aMatrix; // breakpoint 1 } } }
I run it until I hit breakpoint 2 and I expect to see
DemandSupplyMatrix
being null in the debugger. Instead, it appears to have been already instantiated, and I have never hit breakpoint 1... I really need to debug the getter but it seems that I have no chance. Is this normal or am I missing something?Bogdan.
Hi, I tried this on VS 2008 C# Express, and you're right, breakpoints inside getters never get hit. If you turn the getter into a normal method, it works fine. Probably is a bug in VS, properties get translated in methods with a slightly different name... :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
-
Hi, I tried this on VS 2008 C# Express, and you're right, breakpoints inside getters never get hit. If you turn the getter into a normal method, it works fine. Probably is a bug in VS, properties get translated in methods with a slightly different name... :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
-
It is also the suggestion of MS FxCop. Never use an array directly in a property. The suggestion is always change it to a method.
I was very surprised to see breakpoints work fine when the getter properties return a simple variable or a collection such as a List<int>, and they don't when the return type is an array. That doesn't make much sense to me. :omg:
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!