Using Visual Studio with C Creating editing debugging

Using Visual Studio with C# Creating, editing, debugging, and running C# programs

Creating C# Solutions in Visual Studio C# solutions, projects, and code

Using Visual Studio with C# • Visual Studio with C# uses the same IDE (Interactive Development Environment) as • Visual C++ • Visual Basic • Visual F# and Iron. Python • Other. NET languages • VS has many features in common with the Eclipse IDE for Java June 6, 2021 Introduction to the Syntax and Structure of C# Programs 3

Visual Studio IDE • The IDE for Visual Studio provides a • Powerful code editor with Intellisense • Project manager (called the solution explorer) explorer • Interactive debugger • UML class diagramming tool • Code execution profiler • Many other features, some of which we shall see

Installing VS 2019 • VS is large and you may not need/want all components. • It is organized into Workloads (for example Web Apps, C#, VB, C++, F#, etc. ) and you can select those you want C# GUI and console apps

Installing VS 2019 • Also add the individual components you need. Get Help Viewer, compilers, data base tools and anything else you want

Visual Studio 2019 Recent Projects Open Other Project Create New Project

Creating New Project

Visual Studio Solution • Visual studio organizes C# programs into • Solutions (. sln) • Each solution contains one or more projects (. csproj) • Each project has one or more C# source code files (. cs) and possibly other types of files as well • The solution explorer tab in Visual Studio shows the solution structure: Project Source code files June 6, 2021 Introduction to the Syntax and Structure of C# Programs 9

Creating a Console Application • On the File menu, choose New/Project …Project File/New … June 6, 2021 Introduction to the Syntax and Structure of C# Programs 10

Creating a Console App, cont. Code editor window Namespace and class names default as shown – but the names SHOULD be changed June 6, 2021 Introduction to the Syntax and Structure of C# Programs File name SHOULD also be changed here; Do NOT turn in a program named Program (or Project 3, etc. ) 11

Running a Console Application Use these menu choices to run. The F-key combinations and toolbar buttons can also be used. June 6, 2021 Introduction to the Syntax and Structure of C# Programs 12

Add a New Class to the Project Use this to add a new class or some other project item Add new class June 6, 2021 Introduction to the Syntax and Structure of C# Programs 13

Adding a New Class Name the class file – e. g. Student. cs June 6, 2021 Introduction to the Syntax and Structure of C# Programs 14

Tailoring Visual Studio Setting IDE options to your preferences

Setting Options • Use Tools/Options to get to Options Dialog May also set options for error messages, output, debugger, etc. Select Font Select Syntax Coloring Choices Preview your selection June 6, 2021 Introduction to the Syntax and Structure of C# Programs 16

More Options Set Default Location for new Projects – analogous to a Java workspace June 6, 2021 Introduction to the Syntax and Structure of C# Programs 17

More Options Show line numbers in code editor June 6, 2021 Introduction to the Syntax and Structure of C# Programs 18

More Options Set size of tabs for indenting things in the code editor – 4 is common June 6, 2021 Introduction to the Syntax and Structure of C# Programs 19

More Options Set your preferences for automatic indenting – example shows the effect for the selected item June 6, 2021 Introduction to the Syntax and Structure of C# Programs 20

More Options Preferences for vertical spacing – makes program more readable. Example shows results. June 6, 2021 Introduction to the Syntax and Structure of C# Programs 21

More Options Set preferences for horizontal spacing to improve readability. See results in example to left. June 6, 2021 Introduction to the Syntax and Structure of C# Programs 22

Save/Restore Options This can be used to set options on one machine and use them on other machines as well. June 6, 2021 Introduction to the Syntax and Structure of C# Programs 23

Visual Studio Debugger Elementary Debugging Tools in VS June 6, 2021 Odds and Ends 24

Debugging • Three types of errors that developers see frequently • Syntax errors: compile time issues • Execution time program crashes • Program logic errors – program runs successfully but does not produce desired results June 6, 2021 Odds and Ends 25

The VS Debugger • The debugger is a set of tools that can be used to • monitor a running program • inspect object state (values) during execution • collect execution history so that at any point we know how we got to that point • and more • The debugger may be used to help the developer gather information needed to deal with runtime errors, not syntax errors

VS Debugger • The debugger can be used for some quite sophisticated debugging situations such as • Distributed solutions with parts running on separate computers in diverse locations • Parallel algorithms running on multiple processors within a single computer (or a super computer) • Debug suspected bugs within VS itself • Monitor memory, registers, and so forth • The discussion here is limited to much more common debugging scenarios June 6, 2021 Odds and Ends 27

“Watching Code Run” • To use the debugger’s features, execution must be started with “Start Debugging” Start Debugging June 6, 2021 Odds and Ends 28

Code Breakpoints • In debug mode, one may pause program execution at a specified point called a breakpoint • Once paused, one can inspect the current values of variables, properties, and so forth • One may step through subsequent instructions one at a time • One may inspect program history to determine what execution path led to this point (a method may have been called from many different places, for example, and it may be helpful to know where it was called this time) • One may even modify something and continue June 6, 2021 Odds and Ends 29

Breakpoints • The easiest way to set a breakpoint on or off is to click in the margin to the left of the line number Click here to toggle the breakpoint on/off June 6, 2021 Odds and Ends 30

Breakpoints • One may set as many breakpoints as desired • When execution reaches a breakpoint • Execution pauses at that line (it has not been executed - yet) • Program runs full speed up to the first breakpoint • At the breakpoint, one may inspect the values of objects at that point in the program • Easiest way to do this is to hover the object with the cursor, and a tool tip will reveal the value • For a complex object such as an array, one may have to expand the tool tip to see the details June 6, 2021 Odds and Ends 31

Yellow arrow marks place where execution is paused Example Tooltip shows current value of str. From – line 20 has not been executed yet. Hovering over a different variable shows its value – as of line 20 June 6, 2021 Odds and Ends 32

Debugging Toolbar Run to next breakpoint Stop debugging Step Into – a method one is trying to execute on this line – to watch it run, a line at a time Step over – execute the line at full speed but pause on the following line Step Out Of – execute rest of this method and return to the calling method; most useful when in code you did not write June 6, 2021 Odds and Ends 33

Step Through Code and Watch it Run • Execution point moves to next line and result of the executed line can be observed June 6, 2021 Odds and Ends 34

Point and Click Debugging (new in VS 2017) • Sometimes, when stopped at a break point or while stepping through subsequent instructions, one may prefer to “jump ahead” ahead to a later line rather than stepping over several lines one line at a time • This can be accomplished in several ways such as: • Add another break-point at the later line • Use a new Point-and-Click feature added in VS 2017 • The new Point-and-Click feature is quicker and involves only a single step as shown on the next slide

Point and Click Debugging (new in VS 2017) • When stopped in a break state under the debugger, a light green “run execution to here” glyph subtly appears next to a line of code where your mouse is hovered • Move your mouse to the glyph and click the button. Your code will run and stop on that line the next time it is hit in your code path • Run to Click works within the same method, method between different methods, methods and within loops


Call Stack • The Call Stack shows the program’s execution history • For example: Where we are now • This shows we got to the Miles. Per. Gallon constructor from a call to it on Line 17 of Main Where that method was called June 6, 2021 Odds and Ends 38

Locals Watch • The Locals Watch window shows values of all locally defined variables at the point in time where execution is temporarily frozen • Stepping through allows one to watch these values change June 6, 2021 Odds and Ends 39

Autos Watch • The Autos Watch Window shows information similar to the Locals Watch Window June 6, 2021 Odds and Ends 40

Disassembly • One can even see the pseudo assembly language code that has been generated … June 6, 2021 Odds and Ends 41

More On Breakpoints • Right-clicking on a breakpoint brings up a context menu that allows one to make breakpoints work in more sophisticated ways June 6, 2021 Odds and Ends 42

Conditional Breakpoints • Sometimes, a breakpoint may be in a loop and you may not want to pause every time through the loop, but you do want to break on specific passes • Specify a condition that determines when to break User-specified condition June 6, 2021 Odds and Ends 43

Conditional Breakpoints • You may want to stop after the breakpoint is hit a certain number of times June 6, 2021 Odds and Ends 44

Intelli. Trace • Using the techniques described so far, one may watch a program run, examining variable values as the program advances • One can only go forward in execution or quit debugging after a pause • One cannot go backward if the result of some previous step has been forgotten • Intelli. Trace allows one to move backwards as well as forward • Intelli. Trace “records” the state of the program as it executes • One may “back up” and review a previously recorded state June 6, 2021 Odds and Ends 45

Using Intellitrace Open the Intellitrace Tab History to this point June 6, 2021 Odds and Ends 46
- Slides: 46