Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging a C# Object Initializer

Does anyone have any tips for debugging exceptions in a C# object initializer block? The object initializer syntax is basically all or nothing, which can make it especially difficult to troubleshoot inside of a LINQ query. Short of breaking the object creation out to a separate method, is there anything I can do to see which property setter is throwing an exception?

like image 932
technomalogical Avatar asked May 18 '09 15:05

technomalogical


People also ask

What is debugging in C?

Debugging is a methodical process of finding and reducing the number of bugs (or defects) in a computer program, thus making it behave as originally expected.

What are the 4 steps in debugging?

Isolate the source of the bug. Identify the cause of the bug. Determine a fix for the bug. Apply the fix and test it.


1 Answers

Disabling the option to step over property setters [Step over properties and operators (Managed Only)] can allow you to step into the property setters.

Otherwise, the best option is often to just break it out and debug it outside of the LINQ statement. You can wrap your initialization parameters into an anonymous type in linq, and construct your object(s) outside of your linq statement for debugging purposes.

like image 155
Reed Copsey Avatar answered Sep 25 '22 00:09

Reed Copsey