Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Visual Studio with C#

Similar presentations


Presentation on theme: "Using Visual Studio with C#"— Presentation transcript:

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

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

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

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

5 Visual Studio 2017 Need ID to sign in – to have same settings on other computers A number beside the flag indicates updates available Create new project or open existing project Scroll down to see a list of short videos that demo VS 2017 features Recently opened projects

6 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 September 17, 2018 Introduction to the Syntax and Structure of C# Programs

7 Creating a Console Application in VS2017
On the File menu, choose New/Project …Project File/New … September 17, 2018 Introduction to the Syntax and Structure of C# Programs

8 Can target a specific .NET version
Console Application Project Name Location of Solution Folder Solution name same as project by default September 17, 2018 Introduction to the Syntax and Structure of C# Programs

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

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

11 Add a New Class to the Project
Use this to add a new class or some other project item Add new class September 17, 2018 Introduction to the Syntax and Structure of C# Programs

12 Adding a New Class Name the class file – e.g. Student.cs
September 17, 2018 Introduction to the Syntax and Structure of C# Programs

13 Tailoring Visual Studio
Setting IDE options to your preferences

14 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 September 17, 2018 Introduction to the Syntax and Structure of C# Programs

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

16 More Options Show line numbers in code editor September 17, 2018
Introduction to the Syntax and Structure of C# Programs

17 More Options Set size of tabs for indenting things in the code editor – 4 is common September 17, 2018 Introduction to the Syntax and Structure of C# Programs

18 Introduction to the Syntax and Structure of C# Programs
More Options Set your preferences for automatic indenting – example shows the effect for the selected item September 17, 2018 Introduction to the Syntax and Structure of C# Programs

19 Introduction to the Syntax and Structure of C# Programs
More Options Preferences for vertical spacing – makes program more readable. Example shows results. September 17, 2018 Introduction to the Syntax and Structure of C# Programs

20 Introduction to the Syntax and Structure of C# Programs
More Options Set preferences for horizontal spacing to improve readability. See results in example to left. September 17, 2018 Introduction to the Syntax and Structure of C# Programs

21 Introduction to the Syntax and Structure of C# Programs
Save/Restore Options This can be used to set options on one machine and use them on other machines as well. September 17, 2018 Introduction to the Syntax and Structure of C# Programs

22 Visual Studio Debugger
Elementary Debugging Tools in VS September 17, 2018 Odds and Ends

23 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 September 17, 2018 Odds and Ends

24 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

25 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 September 17, 2018 Odds and Ends

26 “Watching Code Run” To use the debugger’s features, execution must be started with “Start Debugging” Start Debugging September 17, 2018 Odds and Ends

27 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 September 17, 2018 Odds and Ends

28 Click here to toggle the breakpoint on/off
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 September 17, 2018 Odds and Ends

29 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 over 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 September 17, 2018 Odds and Ends

30 Example Yellow arrow marks place where execution is paused
Tooltip shows current value of strFrom – line 20 has not been executed yet. Hovering over a different variable shows its value – as of line 20 September 17, 2018 Odds and Ends

31 Debugging Toolbar Stop debugging Run to next breakpoint
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 September 17, 2018 Odds and Ends

32 Step Through Code and Watch it Run
Execution point moves to next line and result of the executed line can be observed September 17, 2018 Odds and Ends

33 Point and Click Debugging (new in VS2017)
Sometimes, when stopped at a break point or while stepping through subsequent instructions, one may prefer to “jump 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

34 Point and Click Debugging (new in VS2017)
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, between different methods, and within loops

35

36 Where that method was called
Call Stack The Call Stack shows the program’s execution history For example: This shows we got to the MilesPerGallon constructor from a call to it on Line 17 of Main Where we are now Where that method was called September 17, 2018 Odds and Ends

37 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 September 17, 2018 Odds and Ends

38 Autos Watch The Autos Watch Window shows information similar to the Locals Watch Window September 17, 2018 Odds and Ends

39 Disassembly One can even see the pseudo assembly language code that has been generated … September 17, 2018 Odds and Ends

40 More On Breakpoints Right-clicking on a breakpoint brings up a context menu that allows one to make breakpoints work in more sophisticated ways September 17, 2018 Odds and Ends

41 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 September 17, 2018 Odds and Ends

42 Conditional Breakpoints
You may want to stop after the breakpoint is hit a certain number of times September 17, 2018 Odds and Ends

43 IntelliTrace 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 IntelliTrace allows one to move backwards as well as forward IntelliTrace “records” the state of the program as it executes One may “back up” and review a previously recorded state September 17, 2018 Odds and Ends

44 Open the Intellitrace Tab
Using Intellitrace Open the Intellitrace Tab History to this point September 17, 2018 Odds and Ends


Download ppt "Using Visual Studio with C#"

Similar presentations


Ads by Google