Exploring classes and methods in VS - c#

Im looking the name definition of this process so I can learn it properly. When I debug code in VS ill hover over a var before breakpoint to see what data is held in it and this window pops up which allows you to see methods and properties, there is usually a base item in it too. Is this called intellisense or something else?
I dont understand the difference between a lot of the items an how you find the thing (function?) you want - especially when things seem to be many levels deep in this window.
Thanks C

The window that pops-up is called a DataTip, the parts of a DataTip that let you examine the representation of a value are called Visualizers.
See Debugger Basics.

Related

Find all unused code in my project (Rider/VSCode)

Goal
Ideally, our projects contain no unused namespaces / methods / functions and classes.
It is fairly simple to find the number of usages of all of these, for instance;
Has two usages while the following class is never used:
Such code can remain unnoticed for quite some time, while providing unwanted overhead to the total solution.
What I'd like to achieve is an overview of all unused code, so developers can easily assess what should and shouldn't stay in the solution.
Question
Of course we're not going to manually search for these...
How can we find all unused code?
To find unused code in Rider:
Select Code | Inspect Code in the application menu.
Choose a scope to inspect (solution, project, or a custom scope).
In the Inspection Results window, group inspections by issue category (and optionally by issue type).
Focus on issues under Redundancies in code and Redundancies in symbol declarations:
Alternatively, as you read or edit your code in the editor, you may encounter specific unused code warnings that Rider shows you. If you want to find all issues similar to one specific issue:
Press Alt+Enter to display Rider's code inspection pop-up.
Press Right arrow to expand options for the current inspection.
Press Right arrow to expand the Find similar issues submenu.
Select a scope to find issues.
Work with the Inspection Results window as shown above.

Debugging: get path of execution

I was wondering if there is any way to return the path that Visual Studio takes when running through a program. For example, without setting breakpoints, is it possible to know exactly what happens when I click a button on a web app I created? Like if I clicked a button that had an action, is there some kind of info that Visual Studio can spit out that tells me what happened? (which functions in which files were used, etc. Essentially the "code path").
To add on to my question, I'd like to use something like this to help debug an issue. I want to know exactly what is happening when I perform some kind of action (button click in my example). If I know every "code path" I can better address the issue I'm having.
If I understand correctly your question, you're looking for Environment.StackTrace
The StackTrace property lists method calls in reverse chronological order, that is, the most recent method call is described first, and one line of stack trace information is listed for each method call on the stack. However, the StackTrace property might not report as many method calls as expected due to code transformations that occur during optimization.
Something else you can do is adding debug log entries to know what's is going on. It may be more useful since you could add extra custom data.
Not sure if you already have a log framework on your project. If you don't have any log4net could be a good option
Converting my comment to an answer so there is an answer that addresses the original question:
Use a performance profiler, which is designed to observe execution paths (typically to help you find bottlenecks, etc., but it will work fine for your purposes too). A good performance profiler will let you dig down and see, for any method, what methods it calls, how frequently, etc.
You can print out a call stack results to the output window in Visual Studio. You will still need to use breakpoints but the ones that doesn't stop the application. To do that:
Find the place in your code when you would like to get the stack printed out. For instance at the end of button clicked handler.
Place a break point there.
Right click the breakpoint. From the context menu select "When Hit..." option.
In the window that will show up select "Print a message:" checkbox. It will enable a textbox. In the textbox type in: $CALLSTACK.
Check the "Continue execution" checkbox if you don't want to stop the execution while printing the results.
I don't remember if the "When Hit..." option is available in the Express edition of Visual Studio.
There are also some other data you can print out without breaking execution, e.g.: previous function, process id, etc. The details are actually available in the "When Hit..." window.

Recover code deleted by Visual C# Form Designer?

So, I made a program in Visual C# 2010 Express. Finished enough to work, and pretty darned good for my first attempt at the language. But then I foolishly decided, "Hey, this program needs a close confirmation dialog before I send it out!" So I write the function, and then I go into the auto-generated code to bind said function appropriately.
KABOOM!
Now, the form designer shows a blank form, and all those shiny components have all been erased from both the designer and the auto-generated code. Which, in such a simple program, is about 80% of the work I put into it.
Now, in NetBeans, I would right-click the file's tab, and just go back to a previous version, maybe losing about 15 minutes of work. This is a pretty obvious concept, I'm just not sure where to find file history in Visual Studio. So, where is the equivalent so I can get my work back?
(I've also managed to get my code back by just undoing a whole bunch - but the designer doesn't see it, and neither does the compiler, so it still builds to a blank form.)
Sadly, you've just learned a couple of things.
One is to never, ever modify designer-generated code. In fact, C# has a concept called partial classes which allow you to modify a generated class without having to modify the generated file.
Second, that Visual Studio in and of itself has no built in source code control -- you need to choose the one you like and be diligent in using it. Some of the most popular are git, subversion, mercurial, and Team Foundation Server. I personally use svn, but the others are good too. Avoid something called Visual SourceSafe (VSS).
From the sound of it, I'd say you tried ctrl-z until some point in your code, but not in the designer?
Performing multiple "undo's" in the code-behind file where you put your code is one thing, performing "undo's" in the designer is another.
Hopefully, you have not closed visual studio yet, and viewing your now blank form in design view, and trying a bunch of ctrl-z presses there, might do the trick.
Actually the controls are not lost from Design code or Initialize
Component() ,make sure that whether check
Initialize Component() { this.controls.add("Where the control was ")
}
Example:
1-this.Controls.Add(this.panel1.label1); 2-this.Controls.Add(this.panel1.label2);
either in form you can write like below
should write in the Initialize Component()
this.Controls.Add(this.label1);
I had a similar effect: the code wasn't lost because the project compiled perfectly but I only got a blank form in designer.
This was because the form was inherited from another form, and I added in the Load event of the base form the line: this.MdiParent = Application.OpenForms("BaseForm");
After deleting this line (and recompiling) I could access the design of the descendig forms again.
One thing you could do, it won't help you this time, but may be a good idea for the future - if you copy your code alot (like I do), get one of those clipboard savers - like ClipTrap (which is lightweight, simple, and great).
Then, if you realize you can't undo what you did, then you get a second chance by running through the "trapped" text to see if what you want might just be there.
Another good option is to try something like AutoVer, which will save a copy of any changed file (or files in a folder) every so often as you like. This could provide you with a backup of your codefile, or even the entire project - every five minutes, or every minute as long as something has been changed. Awesome program. This is a certain fashion of source control, or at least source backup.
(And no I'm not the authour of either application)
However, for your particular problem, if the code is still there in your designer.cs page - maybe you just erased a reference or a namespace. Make sure the namespace matches, and make sure your in your form's code file, that the InitializeComponent(); function is being called (that's what places all the designer's controls onto the form, you know).
In the code behind file click 'undo', not in the designer window. Accept the warning, and when the code comes back, the form should be back to normal, Visual Studio 2015.

C# and Visual Studio?

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.

Debugging C# applications

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

Categories