Is it possible to somehow get the debugger context in F# interactive? For instance, if I hit a breakpoint in C# project then I would like to work with local variables in F# interactive, the same way I can work with them in immediate window. Is there any way to do so? Or do I need to create a debugger visualizer for that?
Neither. You need to create an Expression Evaluator for it. Not sure if C# can be extended, but there is a sample in the VS SDK (the 2008 one at least).
I've make very similar tool for Python, so I'm just using Python shell to evaluate some expressions like in Immediate Window. For this I've created a very simple VS Add-In and Python helper script.
You can find sources on github - https://github.com/dp0h/VsImmediate
Related
When I'm debugging a C# program in Visual Studio and I've stopped at a breakpoint, is there a way to plug in the F# REPL and evaluate expressions at that scope?
If not, is there any other easy way to evaluate and run arbitrary code while in the middle of a debugging session?
You can use the Immediate Window for this:
The Immediate window is used to debug and evaluate expressions, execute statements, print variable values, and so forth. It allows you to enter expressions to be evaluated or executed by the development language during debugging. To display the Immediate window, open a project for editing, then choose Windows from the Debug menu and select Immediate, or press CTRL+ALT+I.
I'm looking for something like JS/Ruby engines that enable you to run commands from terminal. Something like to what LINQPad do but much more simple.
I know C# is not design to be used a a script but would be helpful to have, for example, a mono console application with Main() awaiting for commands came from terminal.
May can I implement such a thing?
Have you tried the interactive C# shell?
There is also a GUI version called gsharp
More here
You can try Jon Skeet's Snippy. Source code is also available so you can tweak it or just use as a sample to create your own.
Also, Visual Studio has build in solution - "Debug->Windows->Immediate". Just create empty console project and start typing commands in the Immediate window.
I'm trying to learn C# from some YouTube videos, but I have a few concerns. I started with JavaScript, wanted to learn a software language, so I learned some C++, but then I found out that C# is better for GUI programming, so now I'm going with that.
The problem is that I feel like VS is doing a lot of things I should know how to do but don't... When I add a button or create a new class or something it's all done with VS. I want to know what it's doing, though, so I can do it myself if I need to.
I'm on the video explaining classes, and I was wondering where the header file was. I couldn't find it, so I searched online and, come to find out, unlike C++, C# doesn't have header files. Are the class files automatically linked?
Is it practical to learn how to do everything manually, or is C# done solely in VS anyway? Is there a way to keep track of what files and code snippets are added and created when a button is pressed in VS?
I am bias, but the best way to understand and learn what is going on in a program is to debug it.
Creating a new C# app in Visual Studio will give you the fastest path to this. You shouldn't have to worry too much what Visual Studio is generating for you, because if you want to understand it, simply step through the code in the debugger. You can see exactly what calls are being made, how each of them changes state, etc, etc.
If you make a basic GUI app, then step through the InitializeComponents function, that gives you a really good sense of how VS sets things up for you.
C# can be used outside of VS, but having all the components so you can just hit F10 and starting stepping away in the IDE makes it the best choice.
Well, best way would be to not use the VS Designer, if you want to learn what happens behind the curtains. You may of course code every single line by hand and it still will work. Eg. to add a button:
Form form = new Form(); // create my form
Button button = new Button(); // create the button
button.Text = "Click me!"; // add some text
form.Controls.Add(button); // add the button to the form
Application.Run(form); // start the applications main thread and display the button
There are dozens of tutorials outside, just find the one that suits you best.
The thing is that visual studio is giving you all the preregistered stuff in built. So you just need to build your own class templates the definition will be presented to you default.
So if you create a class call employee it will create basic syntax for you and you need to write the logic directly.
Thanks,
Jigar
C# as a language is used to develop several kinds of applications such as console-based applications, Windows desktop applications and Web applications.
For the former, you will code everything mostly by hand, even if you are using Visual Studio.
For the latter two, since they are GUI based, it's recommended to use Visual Studio so that you can use the drag-and-drop approach as well as have Visual Studio take care of the routine tasks so that you can focus on the logic and actual development.
IMO, you shouldn't try to learn everythin, but anyway - core concepts will broaden your understanding and will surely help you in further dev-life.
If you want what exactly VS do with your code - put some "VCS guard" at top of project. Use Git or Mercurial and you could see exact changes that was applied to each file (commit everything before you going perform interesting action and check changes after). It could help you to track anything that will happens with your project. But for most cases its not very productive to do everything by hands. DevTools were made to remove very bored routine.
Its very useful to know how you can do something without VS installed, but all such tasks is usually not directly related to programming, mainly its some kind of supporting stuff.
Ex. how build a project with msbuild or csc.exe (C# compiler) to setup homebrew continuous integration system.
If you want really deep grasp into .NET - read CLR via C# by Jeffrey Richter. IMO it's far more important to know system guts than to know how to place button on the form without VS-designer.
C# Interactive seems a lot more powerful than the Immediate Window (at least it handles lambda expressions that are often used in LINQ - see Visual Studio debugging "quick watch" tool and lambda expressions), but it looks like it can't be used as a replacement as it doesn't know about the debugger context. Is there a way to have access to the debugger context?
I've seen Getting debugger context in F# interactive and it might require the same things, but maybe there are new/different things available.
http://extendedimmediatewin.codeplex.com/ could be useful but looks quite dead, although someone said on Oct 18 2011 that he was planning to port it to VB.Net ( http://extendedimmediatewin.codeplex.com/discussions/75589 ).
Not a complete solution, but in VS2015, after you've run Initialize Interactive with Project, you can use Debug > Attach to Process to connect to the InteractiveHost.exe process.
You can then debug functions from your project. You won't be able to debug one-off functions in the Interactive window, though, as Kevin explained.
As as side note, https://stackoverflow.com/a/40650343/467110 points out that you may have to have a C# Interactive window open before you open your solution in order to successfully Initialize Interactive with Project
Disclaimer: I work for Microsoft on the Roslyn team.
Unfortunately, we don't have a way to get the debugger context right now. We are aware that it would useful, and as we get more of the core deliverables for Roslyn completed we hope to investigate more and see what can be done.
Inside C# Interactive you can run
System.Diagnostics.Debugger.Launch();
to request a debuger, whereby you will get a window asking you to choose one of the open Visual Studio instances or a new instance to attach. Assuming you used "Load Interactive with the Project" option of Visual Studio - any breakpoints in the open Solution will now be hit when code is ran by Interactive.
If you need the debugger without pre-existing code - you can now insert
System.Diagnostics.Debugger.Debug();
calls into the code you put into Interactive (but it does have to go in one 'chunk', i.e. one press of 'enter'), and you will be able to examine local vars via 'locals' window and run code against interactive context via 'immediate' window.
I´m coming from a PHP background where my debugging "tools" are basically echo, var_dump and exit. While I know the importance of debugging, I never tried to use/learn a debugging tool.
Now I´m learning C# and I think I can´t really program without an extensive knowledge of this area.
So my question is: where can I learn what is and how to do debugging? I know what is a breakpoint (conceptually), but how to use it? How to use Step into and Step over? Basic things like that.
As a related question, there is anything like var_dump in C# (Visual Studio), where I can inspect any object.
I find very difficult and painful to do a foreach for every array/list to see the contents, specially now that I´m still learning the language.
Microsoft has an extensive guide on C# debugging in Visual Studio that might be helpful. VS has a lot of powerful debugging functionality; for example, rather than doing a foreach to see the contents of an array as you were describing, you could set a breakpoint (pausing the execution of the program) and select the variable you wish to see the contents of (array or list or whatever) and see what it contains, without having to write any extra code. Step Into and Step Over can be used to continue execution of the program but only incrementally so that you can continue to see how variables change, where the flow of execution currently is, etc.
This has been covered earlier on StackOverflow:
Best Visual Studio 2008 Debugging Tutorial?
So, your compiled applications can be run in a "debug" mode from which visual studio can monitor the internal workings of the application and even control it.
A break point can be placed just about anywhere in your code by clicking to the far left of the line (kinda in the margin of the visual studio text editor). When that line of code has been reached, the visual studio debugger will actually pause the execution of your program and bring you back to the editor where you can literally hover over a variable or object or whatever and see everything about it.
There is also a "Locals" window available that will give you the break down of all of your locally scoped items - this should pop up by default at the bottom of your screen when debugging.
In debug mode you can navigate the execution of your code line-by-line
F10 will continue with the next line of code.
F11 will attempt to drill down into what ever functions are on the current line of code
Ctrl-D will bring up a "Quick Watch" window giving you all information about the currently selected variable/object.
Once you are in debug mode there are tons of things you can do - in some cases you can even edit the code as you go.
The easiest way to get into debug mode is to use the little "play" button up at the top of visual studio - and when a break point is reached it will enter debug mode and highlight the currently executing line of code.
You can also hit F10 from the editor and your application will be started and paused on the very first line of code.
By comparison, in PHP, you had to actually write "debugging code" into your application - using Visual Studio you can actually monitor the execution of your code without adding a thing to your existing code.
I hope that gets you started.
You might want to also read up on your IDE a bit to. There is a metric ton of stuff in visual studio that will help you navigate your code in ways you never imagined in most PHP editors.
If you've already downloaded Microsoft Visual Studio, you'd might want to check out the Visual C# Express Library available for free over at: http://msdn.com/express/
It's located down the bottom of the page and is very useful. It contains pretty much every answer you might be looking for as a beginner to the C# Language. ...Welcome to C#, my friend :-D