C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
First: Please start up your version of Visual Studio and create a new C# console application.
C# program that uses local variables
using System;
class Program
{
static void Main()
{
// A.
// An integer is local.
int cat = 1;
// B.
// A string is local.
string dog = "cute";
// C.
// An array is local.
bool[] b = new bool[]
{
true,
false,
true,
true
};
}
}
Then: Click on it in the line you want to debug. The red circle on the left is where you need to click to set the debugging breakpoint.
And: The red block over the code line is where the debugger will open. We must first execute the program.
And: The icons on your setup will look a little different depending on your preferences and OS.
Next: Open the Locals window. You can open the Locals pane in more than one way. Go to Debug -> Windows -> Locals.
Note: Many of these options are advanced and not needed for most debugging purposes.
Properties in debugger: Visual Studio
Name:
The name of your variable.
Value:
The data stored in your variable.
Type:
The data type of your variable, such as int, string, bool[].
Text Visualizer:
Allows you to see the plain text value in the variable.
XML Visualizer:
For XML files. You can visualize XML data with this function.
HTML Visualizer:
Use to view the data in Internet Explorer as HTML.
Expression:
This is on the Visualizer windows. Shows the variable name.
Note: Using these options takes practice. It is hard to learn them at the same time you are learning an entire language.
Step options: Visual Studio
Step Into, F11:
Use Step Into to go inside your method and stop immediately inside it.
Step Over:
This also moves to the next step in your code.
It doesn't, however, break inside any methods.
Step Out:
Ignores the rest of the current method and goes to the calling method.
Note: The screenshot shows the local arrays. Each element in the array is referenced by an index.
Tip: Look inside an array by clicking on the + box. You can browse all elements.
Array elements
- b - name of the array
[0] true - first value in array
[1] false - second value in array
[2] true - third
[3] true - fourth
And: With break points, we can write messages to the console, or keep counts of how many times they are hit.
Method 1: In your ASP.NET website, open Web.config and change the compilation tag's debug attribute to false.
Method 2: In Windows applications, go to Project -> MyApplication Properties. Then change the Configure dropdown to Release.
Method 3: The third way is to change the toolbar dropdown when it is available.
And: This depends on the program. More complex, algorithmic programs tend to become still slower.
Benchmark: For benchmarking, please run outside of the debugger. For the best benchmarks, do not involve Visual Studio at all.
BenchmarkThen: When the breakpoint is hit, right click on its value in the Locals window. And change its value to 3.
Result: The value "true" will be printed twice. We edited the value to be 2 and then 3.
Program for Edit Value demonstration: C#
using System;
class Program
{
static void Main()
{
int value = 2;
if (value == 2)
{
Console.WriteLine(true);
}
if (value == 3)
{
Console.WriteLine(true);
}
}
}
Output
true
true
Note: This doesn't involve the VS debugging environment. So they can be used when testing release builds and for performance testing.
ConsoleTip: The benefit to these methods over the Console methods is that they are removed in Release builds of your application.
Debug.Write