Adding an element to a collection while debugging (visual Studio) - c#

I have a question regarding debugging using Visual Studio.
We all know that when debugging you can set breakpoints and have some variables watched. These variables can be editted too. (So you can change their values)
In the case you have a collection (that has for example two elements, you can see the elements and change their values)
But can you add or delete elements of that collection? Is there a way to do this from your watch window?

You can do this from the Immediate Window (Ctrl-Alt-I)
If I run this code and stop on a breakpoint right after this:
var list = new List<int>();
list.Add(1);
list.Add(2);
I can type list.Add(3) in the Immediate Window (and press Enter to run it). If I then type ? list (in the Immediate Window) and press Enter, it will show that the 3rd element has been added.

Yes, there is a way to add or delete elements when you are debugging. When you are in debug mode in Visual Studio, you can simply add the code you need like the way you do while writing code and debug. You can also watch values in watch window. (Its in Microsoft visual studio community 2017). If you are using visual studio 2015, I think there is a option to pause below menu bar while debugging to add extra code in debug mode.
Actually, you will not need watch window. You can simple add code in editor. Alternatively, there is Immediate window(ctrl + Alt + I) to check on that.

You can also right click any variable, enter "Quick watch" and in expression text box evaluate expressions such as Add method on a list.

Related

Is there a "Go To Variable Type Declaration" function for C# in Visual Studio 2015 or a free plugin which does it?

void MagicalFunction(MagicalType magic)
{
...
magic.Poof("something");
...
var foo = magic.GetFoo();
...
}
Pressing the hotkey on variable magic would navigate to definition of type MagicalType.
Pressing the hotkey on foo would go to definition of type Foo which is not directly visible here because of type-inference.
Resharper plugin has this functionality (called Go To Type of Symbol) , but is there a built-in alternative or a free-extension that does this?
Right click on the "var" keyword, select "Go to definition" from context menu, and it will take you to the type definition of the inferred type of the variable. I have some tools installed, like Productivity Power Tools which were mentioned, so not sure if this option is available through clean VS2015.
Edit:
You can also with cursor on the "var" keyword press Ctrl-F12 (Go to Implementation), if you prefer to use keyboard. Ref: https://www.youtube.com/watch?v=xWcQhF-1hxA
Sort of.
You could press F12 on "magic" to get to its definition, and then F12 again to get to its class.
Also, while you can't get anything meaningful by F12 on "foo" since it would just highlight the line you're already on, if you F12 from the "var" immediately prior to foo, it will jump you to the Foo class, even though that type is being inferred.
The function you are looking for is "Navigate To".
For some weird reason, the keyboard shortcut was removed from VS 2015. You can see this by going into the Edit menu. The option will be there but has no shortcut assigned to it.
You can fix this manually. The default was:
CTRL + ,
Go to Tools > Options > Environment > Keyboard, search for "Edit.NavigateTo" and reassign the shortcut. You can then place the cursor on the variable and hit the shortcut and a tiny window will overlay on the top right hand corner with possible candidates, one of them being the type definition.
Productivity Power Tools has a feature "Ctrl + Click Go To Definition
This extension gives the editor a web browser by adding clickable hyperlinks to symbols in your code as you hold down the Ctrl key."
https://visualstudiogallery.msdn.microsoft.com/d0d33361-18e2-46c0-8ff2-4adea1e34fef
it deosn't work in the first case but it works very well when you ctrl click "var" in the second case

Resharper - Disable 'help' when using "prop" shortcut in C#

I'm getting quite annoyed with a feature of Resharper that I just cannot find how to disable independently.
With Resharper turned off, whenever I type prop in VS2015 and press TAB, I get the following auto-generated code:
public int MyProperty { get; set; }
and I'm then able to switch between int and MyProperty repeatedly by pressing TAB again.
I'm also able to use autocomplete to fill in the variable type as I type.
For example, lets say I wanted to create a property called "test" of type "string", I would do the following:
type prop
press TAB to generate the property code template
type stri
press TAB to autocomplete the variable type with string
press TAB to move to the variable name placeholder
type test
press Return to finish
perfect.
However, with Resharper enabled, whilst steps 1,2 and 3 still work, it all goes to pot after that!
If I press TAB to try and autocomplete the variable type, the cursor simply moves over to the variable name, leaving the variable type as stri.
If I then press TAB (or even SHIFT+TAB) again to try and get back to it, it simply finishes the line.
To make things clearer, I've included two gifs demonstrating my problem.
This first one shows what happens with Resharper disabled:
Whilst this one illustrates the frustration I'm currently experiencing with Resharper enabled:
I understand this is an old(er) question and already has an answer; however, I wanted to provide a solution for future SO readers that would allow one to continue using Visual Studio's IntelliSense as opposed to Resharper's.
I had the same issue as the OP (original poster) and found the issue to be caused by a conflict with Resharper's Live Templates. With Resharper enabled, the resolution of prop after Tab + Tab, resolves Resharper's prop Live Template and not Visual Studio's prop snippet. You might have notice that in OP's 2nd screen cap (the one with Resharper enabled) that the resolution of prop has a datatype shown as TYPE instead of int. This is because Resharper's Live Template has set this parameter name to TYPE, while Visual Studio's snippet has the same parameter set to int by default. This is what lead me to look at Resharper's Live Templates for a resolution; lo and behold turning off the prop Live Template fixes the issue.
Go to Resharper's Template Explorer (Resharper -> Tools -> Templates Explorer) you will see there is a Live Template named prop. Simply clear the check box for the prop Live Template and Visual Studio's prop snippet resolution, and datatype resolution for that matter, will start working again.
It looks like you have an intellisense setting conflict.
Re-Enable Resharper and then change this setting:
Resharper->Options->IntelliSense->General
Change the selection from "Visual Studio" to "Resharper", hit Save and the desired behaviour should be yours.
I find those shortcuts still work without disabling anything however sometimes this functionality stops working.
Currently using R# 2018.2 with the default keyboard mappings set to IntelliJ IDEA scheme (as I also use java) and the shortcuts are all available, but when they stop working closing all tabs and reopening the tab you are working on normally fixes it.
No restart or reset is needed.

For all indices in visual studio shows Hex value? [duplicate]

I'm using Visual Studio 2008 and I have just noticed that the debugger is displaying integer values as Hex when I hover over variables and also in the immediate window. I guess I must have hit a shortcut key accidently or something.
Anyone had this before? How do I set it back to display in decimal?
Right-click your Watch Window or Immediate Window and uncheck Hexadecimal Display option.
You can also choose hexadecimal or decimal display on a per-variable basis in the Visual Studio watch window by appending a debugger format specifier to the variable name. In the watch window, enter:
myInt,h
myInt,d
The other very useful format specifiers are ac (see footnote) for 'always calculate', and nq for displaying with 'no quotes.' They can be used together:
my_string_func(),ac,nq
nq is useful inside DebuggerDisplay attributes, which can appear on a class:
[DebuggerDisplay("{my_string_func(),nq}")]
class MyClass
{
/* ...example continues below... */
...or on one or more field(s) inside a class:
[DebuggerDisplay("{some_field,nq}", Name="substitute name here")]
int an_integer;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
String some_field;
}
http://msdn.microsoft.com/en-us/library/e514eeby(v=VS.100).aspx
note that earlier versions of the MSDN doc page incorrectly said 'Ac' (with a capital 'A')--which doesn't work
There is a Hex button shown when Visual Studio is run in Debug mode to enable/disable the Hex display
Right-click on client space of almost every debug window (except Immediate Window) - watch/locals/autos/threads/call stack - and uncheck "Hexadecimal Display" option.
There's also a "Hex" button in debug toolbar (right to "Step Over" by default) when debugging.
In Visual Studio 2010 I also saw it in the Debug toolbar, it was highlighted in yellow 'Hex', I just clicked it and it returned to (normal) decimal values
Visual Studio 2017
Decimal vs. hexadecimal display is controlled only from the Watch dialog.
Break after setting the variable.
Right mouse click the variable and select "Add Watch" or "QuickWatch"
Right mouse click the line in the Watch dialogue.
Uncheck "Hexadecimal Display"
The display will now be in decimal.
In the immediate window you can uncheck the Hexadecimal Display option.

Visual Studio: How to show Overloads in IntelliSense?

Once code has been written, the only way I know of to view the overloads for a method is to actually edit the method by deleting the Parenthesis () and reopening them.
Is there a shortcut key that I could press to activate this instead of having to edit my files?
For an example, please reference the ShowDialog Overload screen shot below:
With your cursor inside the parentheses, use the keyboard shortcut Ctrl-Shift-Space. If you changed the default, this corresponds to Edit.ParameterInfo.
Example:
Ctrl+Shift+Space shows the Edit.ParameterInfo for the selected method, and by selected method I mean the caret must be within the method parentheses.
Here is the Visual Studio 2010 Keybinding Poster.
And for those still using 2008.
Tested only on Visual Studio 2010.
Place your cursor within the (), press Ctrl+K, then P.
Now navigate by pressing the ↑ / ↓ arrow keys.
The default key binding for this is Ctrl+Shift+Space.
The underlying Visual Studio command is Edit.ParameterInfo.
If the standard keybinding doesn't work for you (possible in some profiles) then you can change it via the keyboard options page
Tools -> Options
Keyboard
Type in Edit.ParameterInfo
Change the shortcut key
Hit Assign
It happens that none of the above methods work. Key binding is proper, but tool tip simply doesn't show in any case, neither as completion help or on demand.
To fix it just go to Tools\Text Editor\C# (or all languages) and check the 'Parameter Information'. Now it should work
Great question; I had the same issue. Turns out that there is indeed a keyboard shortcut to bring up this list: Ctrl+Shift+Space (a variation of the basic IntelliSense shortcut of Ctrl+Space).
The command Edit.ParameterInfo (mapped to Ctrl+Shift+Space by default) will show the overload tooltip if it's invoked when the cursor is inside the parameter brackets of a method call.
The command Edit.QuickInfo (mapped to Ctrl+KCtrl+I by default) will show the tooltip that you'd see if you moused over the cursor location.
I know this is an old post, but for the newbies like myself who still hit this page this might be useful.
when you hover on a method you get a non clickable info-box whereas if you just write a comma in the method parenthesis the IntelliSense will offer you the beloved info-box with the clickable arrows.
Every once and a while the suggestions above stop working, if I restart Visual Studio they start working again though.
you mean's change overload.
just Press Shift + ↑ / ↓
Mine showed up in VS2010 after writing the first parenthesis..
so, prams.Add(
After doings something like that, the box with the up and down arrows appeared.

How to set conditional breakpoints in Visual Studio?

Is there an easy way to set conditional breakpoints in Visual Studio?
If I want to hit a breakpoint only when the value of a variable becomes something, how can I do it?
Set a breakpoint as usual. Right click it. Click Condition.
When you are using Express edition you can try this:
#if DEBUG
if( fooVariable == true )
System.Diagnostics.Debugger.Break();
#endif
if statement makes sure that in release build breakepoint will not be present.
Visual Studio provides lots of options for conditional breakpoints:
To set any of these you
Set a breakpoint.
Right-Click over the breakpoint, and in the popup menu you select an option that suites you.
These options are as follows:
You can set a condition, based on a code expression that you supply (select Condition from the popup menu). For instance, you can specify that foo == 8 or some other expression.
You can make breakpoints trigger after they have been hit a certain number of times. (select Hit Count from the popup menu). This is a fun option to play with as you actually aren't limited to breaking on a certain hit count, but you have options for a few other scenarios as well. I'll leave it to you to explore the possibilities.
You can Set filters on the Process ID, thread ID, and machine name (select Filter from the popup menu)
Just another way of doing it, (or if you are using express) add the condition in code:
if(yourCondition)
{
System.Diagnostics.Debugger.Break();
}
Set breakpoint on the line
Right clik on RED ball
Chose conditioal breakpoint
Setup condition
Writing the actual condition can be the tricky part, so I tend to
Set a regular breakpoint.
Run the code until the breakpoint is hit for the first time.
Use the Immediate Window (Debug > Windows > Immediate) to test your expression.
Right-click the breakpoint, click Condition and paste in your
expression.
Advantages of using the Immediate window:
It has IntelliSense.
You can be sure that the variables in the expression are in scope when the expression is evaluated.
You can be sure your expression returns true or false.
This example breaks when the code is referring to a table with the name "Setting":
table.GetTableName().Contains("Setting")
Create a breakpoint as you normally would, right click the red dot and select "condition".
On Visual Studio 6.0
Alt+F9!!!
Set a breakpoint as usual
Right click on the breakpoint and select Condition
You'll see a dialog that says "Breakpoint Condition"
Put a condition in the field e.g. "i==5"
The breakpoint will only get hit when i is 5.
Set a breakpoint as usual.
Right-click on the breakpoint marker
Click "Condition..."
Write a condition, you may use variable names
Select either "Is True" or "Has Changed"
Set the breakpoint as you do normally, right click the break point and select condion option and sets your condition.
Create a conditional function breakpoint:
In the Breakpoints window, click New to create a new breakpoint.
On the Function tab, type Reverse for Function. Type 1 for Line, type 1 for Character, and then set Language to Basic.
Click Condition and make sure that the Condition checkbox is selected. Type instr.length > 0 for Condition, make sure that the is true option is selected, and then click OK.
In the New Breakpoint dialog box, click OK.
On the Debug menu, click Start.
You can control when and where a breakpoint executes by setting conditions. The condition can be any valid expression that the debugger recognizes. For more information about valid expressions, see Expressions in the debugger.
To set a breakpoint condition:
Right-click the breakpoint symbol and select Conditions (or press Alt + F9, C). Or hover over the breakpoint symbol, select the Settings icon, and then select Conditions in the Breakpoint Settings window.
You can also set conditions in the Breakpoints window by right-clicking a breakpoint and selecting Settings, and then selecting Conditions.
In the dropdown, select Conditional Expression, Hit Count, or Filter, and set the value accordingly.
Select Close or press Ctrl+Enter to close the Breakpoint Settings window. Or, from the Breakpoints window, select OK to close the dialog.
Breakpoints with conditions set appear with a + symbol in the source code and Breakpoints windows.
If you came from Google, this answer might be what you are searching for.
Click Debug> New BreakPoint > Function Breakpoint
there choose the conditional Breakpoint.

Categories

Resources