Execute selection in C# program.

This is something that I think sometimes would be useful, but I don't know how feasible it is, as I see some problems with the idea.

If I make a text selection in the main method of "C# program" query and click "Execute", sometimes would be useful if the additional methods and classes outside the main method were available so that it doesn't fail to compile because the classes weren't part of the selected text.

All it would need to do is to make the text selection the new main method, but keep everything the same.
This could work even if the text selection was someplace else than the main method. The selected text could still be the main method.

However, there are some concerns which I am not sure what to do with:
* What if the rest of the query doesn't compile?
* What if static initializers or something caused a runtime error to happen (which had nothing to do with the selected text).

The same may be true for the "Clone query" operation with selected text.


  • Right now, I'm prototyping a debugger for LINQPad. Assuming the project turns out to be feasible, it could offer another way to deal with this, i.e., "Debug | Jump to Selection" rather than "Execute Selection". With "Jump to Selection", the entire query would be compiled, and then the execution pointer would jump to the start of the selected text and stop at the end. This would presumably work only for text in the main code block of the main method.

    What are your thoughts on that?
  • Debug inside LINQPad? Single step with variable inspection? That's awesome. I have domain experts who have turned into full time programmers inside LINQPad. They write their domain logic inside LINQPad and ship individual queries off to India for integration into the larger product. Since LINQPad is their only IDE, the debug options are limited.
    They have debugged once or twice inside visual studio express. They will be very glad to hear this. Debugging is right at the top of the wishlist (along with Git integration).

    Is there a technical reason why it only would work inside the main method and not any static method? Couldn't you still just set the execution pointer. It wouldn't make that much difference, but I am asking to learn. "Setting the execution pointer" is pretty far away from my strong suits.

    The practical difference from my suggestion is the ability to execute selection in any other place than the main method.
    My suggestion was that the text selection would become the whole main method in the compiled artifact. I still see some benefits from that, especially as the methods and classes increase in size and the original main method is basically just initializing and calling into stuff.
    This would most of the time work for instance methods as long as they don't access instance members. But if they do, there is some external execution context at work and the user should probably be looking to the debugger anyway (assuming it is coming).

    Expanding on my suggestion after having slept on it: To preserve LINQPads minimalistic style I think it would be best if LINQPad first tried to compile text selection it as "C# statements" and only proceeded to compile using this approach if compiling as "C# statements" failed. This so that initializers will not affect runtime or performance in cases where they are not needed to execute the selection. I think it makes the most intuitive sense to the user.

    These are my initial thoughts. I appreciate being asked and hope that I can contribute more when I have contemplated it some more.
Sign In or Register to comment.