Visual Studio for Mac.NET. Azure DevOps. Azure DevOps Server (TFS) 0. Run on external console - Asp.Net Core app. Fixed in: visual studio 2019 for mac version 8.1 visual studio for mac macos 10.14 project solution. Terman.emil reported Apr 11, 2019 at 03:33 PM.
- Visual Studio extension development. Create add-ons and extensions for Visual Studio, including new commands, code analyzers, and tool windows. Add the SDKs and tools you need to create new commands, code analyzers, tool windows, and language services using C#. Then, share your extension with the community in the Visual Studio.
- Apr 06, 2017 Visual Studio. Visual Studio for Mac.NET. Azure DevOps. Azure DevOps Server (TFS) 2. Cannot debug.net core console application. Windows 10.0 Visual Studio 2017 version 15.1. Dan Ingalla reported Apr 06, 2017 at 08:57 PM. Show comments 6.
In this tutorial for C#, you'll use Visual Studio to create and run a console app and explore some features of the Visual Studio integrated development environment (IDE) while you do so.
If you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.
If you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.
Create a project
To start, we'll create a C# application project. The project type comes with all the template files you'll need, before you've even added anything!
- Open Visual Studio 2017.
- From the top menu bar, choose File > New > Project.(Alternatively, press Ctrl+Shift+N).
- In the left pane of the New Project dialog box, expand C#, and then choose .NET Core. In the middle pane, choose Console App (.NET Core). Then name the file Calculator.
Add a workload (optional)
If you don't see the Console App (.NET Core) project template, you can get it by adding the .NET Core cross-platform development workload. Here's how.
Option 1: Use the New Project dialog box
- Choose the Open Visual Studio Installer link in the left pane of the New Project dialog box.
- The Visual Studio Installer launches. Choose the .NET Core cross-platform development workload, and then choose Modify.
Option 2: Use the Tools menu bar
- Cancel out of the New Project dialog box and from the top menu bar, choose Tools > Get Tools and Features.
- The Visual Studio Installer launches. Choose the .NET Core cross-platform development workload, and then choose Modify.
- Open Visual Studio 2019.
- On the start window, choose Create a new project.
- On the Create a new project window, enter or type console in the search box. Next, choose C# from the Language list, and then choose Windows from the Platform list.After you apply the language and platform filters, choose the Console App (.NET Core) template, and then choose Next.NoteIf you do not see the Console App (.NET Core) template, you can install it from the Create a new project window. In the Not finding what you're looking for? message, choose the Install more tools and features link.Then, in the Visual Studio Installer, choose the .NET Core cross-platform development workload.After that, choose the Modify button in the Visual Studio Installer. You might be prompted to save your work; if so, do so. Next, choose Continue to install the workload. Then, return to step 2 in this 'Create a project' procedure.
- In the Configure your new project window, type or enter Calculator in the Project name box. Then, choose Create.Visual Studio opens your new project, which includes default 'Hello World' code.
Create the app
First, we'll explore some basic integer math in C#. Then, we'll add code to create a basic calculator. After that, we'll debug the app to find and fix errors. And finally, we'll refine the code to make it more efficient.
Explore integer math
Let's start with some basic integer math in C#.
- In the code editor, delete the default 'Hello World' code.Specifically, delete the line that says,
Console.WriteLine('Hello World!');
. - In its place, type the following code:Notice that when you do so, the IntelliSense feature in Visual Studio offers you the option to autocomplete the entry.NoteThe following animation isn't intended to duplicate the preceding code. It's intended only to show how the autocomplete feature works.
- Choose the green Start button next to Calculator to build and run your program, or press F5.A console window opens that reveals the sum of 42 + 119, which is 161.
- (Optional) You can change the operator to change the result. For example, you can change the
+
operator in theint c = a + b;
line of code to-
for subtraction,*
for multiplication, or/
for division. Then, when you run the program, the result changes, too. - Close the console window.
Add code to create a calculator
Let's continue by adding a more complex set of calculator code to your project.
- Delete all the code you see in the code editor.
- Enter or paste the following new code into the code editor:
- Choose Calculator to run your program, or press F5.A console window opens.
- View your app in the console window, and then follow the prompts to add the numbers 42 and 119.Your app should look similar to the following screenshot:
Add functionality to the calculator
Let's tweak the code to add further functionality.
Add decimals
The calculator app currently accepts and returns whole numbers. But, it will be more precise if we add code that allows for decimals.
As in the following screenshot, if you run the app and divide number 42 by the number 119, your result is 0 (zero), which isn't exact.
Let's fix the code so that it handles decimals.
- Press Ctrl + H to open the Find and Replace control.
- Change each instance of the
int
variable tofloat
.Make sure that you toggle Match case (Alt+C) and Match whole word (Alt+W) in the Find and Replace control. - Run your calculator app again and divide the number 42 by the number 119.Notice that the app now returns a decimal numeral instead of zero.
However, the app produces only a decimal result. Let's make a few more tweaks to the code so that the app can calculate decimals too.
- Use the Find and Replace control (Ctrl + H) to change each instance of the
float
variable todouble
, and to change each instance of theConvert.ToInt32
method toConvert.ToDouble
. - Run your calculator app and divide the number 42.5 by the number 119.75.Notice that the app now accepts decimal values and returns a longer decimal numeral as its result.(We'll fix the number of decimal places in the Revise the code section.)
Debug the app
We've improved on our basic calculator app, but it doesn't yet have fail safes in place to handle exceptions, such as user input errors.
For example, if you try to divide a number by zero, or enter an alpha character when the app expects a numeric character (or vice versa), the app might stop working, return an error, or return an unexpected nonnumeric result.
Let's walk through a few common user input errors, locate them in the debugger if they appear there, and fix them in the code.
Tip
For more information about the debugger and how it works, see the First look at the Visual Studio debugger page.
Fix the 'divide by zero' error
When you try to divide a number by zero, the console app might freeze and then show you what's wrong in the code editor.
Note
Sometimes, the app doesn't freeze and the debugger won't show a divide-by-zero error. Instead, the app might return an unexpected nonnumeric result, such as an infinity symbol. The following code fix still applies.
Let's change the code to handle this error.
- Delete the code that appears directly between
case 'd':
and the comment that says// Wait for the user to respond before closing
. - Replace it with the following code:After you add the code, the section with the
switch
statement should look similar to the following screenshot:
Now, when you divide any number by zero, the app will ask for another number. Even better: It won't stop asking until you provide a number other than zero.
Fix the 'format' error
If you enter an alpha character when the app expects a numeric character (or vice versa), the console app freezes. Visual Studio then shows you what's wrong in the code editor.
To fix this error, we must refactor the code that we've previously entered.
Revise the code
Rather than rely on the
program
class to handle all the code, we'll divide our app into two classes: Calculator
and Program
.The
Calculator
class will handle the bulk of the calculation work, and the Program
class will handle the user interface and error-capturing work.Let's get started.
- Delete everything in the
Calculator
namespace between its opening and closing braces: - Next, add a new
Calculator
class, as follows: - Then, add a new
Program
class, as follows: - Choose Calculator to run your program, or press F5.
- Follow the prompts and divide the number 42 by the number 119. Your app should look similar to the following screenshot:Notice that you have the option to enter more equations until you choose to close the console app. And, we've also reduced the number of decimal places in the result.
Close the app
- If you haven't already done so, close the calculator app.
- Close the Output pane in Visual Studio.
- In Visual Studio, press Ctrl+S to save your app.
- Close Visual Studio.
Code complete
During this tutorial, we've made a lot of changes to the calculator app. The app now handles computing resources more efficiently, and it handles most user input errors.
Here's the complete code, all in one place:
Next steps
Congratulations on completing this tutorial! To learn even more, continue with the following tutorials.
See also
-->This tutorial introduces the debugging tools available in Visual Studio for Mac.
Prerequisites
- This tutorial works with the console app that you create in Create a .NET Core console application in Visual Studio for Mac.
Use Debug build configuration
Visual Studio Pair To Mac
Debug and Release are Visual Studio's built-in build configurations. You use the Debug build configuration for debugging and the Release configuration for the final release distribution.
In the Debug configuration, a program compiles with full symbolic debug information and no optimization. Optimization complicates debugging, because the relationship between source code and generated instructions is more complex. The release configuration of a program has no symbolic debug information and is fully optimized.
By default, Visual Studio uses the Debug build configuration, so you don't need to change it before debugging.
- Start Visual Studio for Mac.
- Open the project that you created in Create a .NET Core console application in Visual Studio for Mac. https://ndenergy555.weebly.com/do-i-need-the-eclipse-app-on-mac.html.The current build configuration is shown on the toolbar. The following toolbar image shows that Visual Studio is configured to compile the Debug version of the app:
Console In Visual Studio
Set a breakpoint
A breakpoint temporarily interrupts the execution of the application before the line with the breakpoint is executed.
- Set a breakpoint on the line that displays the name, date, and time. To do that, place the cursor in the line of code and press ⌘ (command+). Another way to set a breakpoint is by selecting Run > Toggle Breakpoint from the menu.Visual Studio indicates the line on which the breakpoint is set by highlighting it and displaying a red dot in the left margin.
- Press ⌘↵ (command+enter) to start the program in debugging mode. Another way to start debugging is by choosing Run > Start Debugging from the menu.
- Enter a string in the terminal window when the program prompts for a name, and then press enter.
- Program execution stops when it reaches the breakpoint, before the
Console.WriteLine
method executes.
Use the Immediate window
The Immediate window lets you interact with the application you're debugging. You can interactively change the value of variables to see how it affects your program.
- If the Immediate window is not visible, display it by choosing View > Debug Pads > Immediate.
- Enter
name = 'Gracie'
in the Immediate window and press enter. - Enter
date = date.AddDays(1)
in the Immediate window and press enter.The Immediate window displays the new value of the string variable and the properties of the DateTime value.The Locals window displays the values of variables that are defined in the currently executing method. The values of the variables that you just changed are updated in the Locals window. - Press ⌘↵ (command+enter) to continue debugging.The values displayed in the terminal correspond to the changes you made in the Immediate window.If you don't see the Terminal, select Terminal - HelloWorld in the bottom navigation bar.
- Press any key to exit the program.
- Close the terminal window.
Set a conditional breakpoint
The program displays a string that the user enters. What happens if the user doesn't enter anything? You can test this with a useful debugging feature called a conditional breakpoint.
- ctrl-click on the red dot that represents the breakpoint. In the context menu, select Edit Breakpoint.
- In the Edit Breakpoint dialog, enter the following code in the field that follows And the following condition is true, and select Apply.Each time the breakpoint is hit, the debugger calls the
String.IsNullOrEmpty(name)
method, and it breaks on this line only if the method call returnstrue
.Instead of a conditional expression, you can specify a hit count, which interrupts program execution before a statement is executed a specified number of times. - Press ⌘↵ (command+enter) to start debugging.
- In the terminal window, press enter when prompted to enter your name.Because the condition you specified (
name
is eithernull
or String.Empty) has been satisfied, program execution stops when it reaches the breakpoint. - Select the Locals window, which shows the values of variables that are local to the currently executing method. In this case,
Main
is the currently executing method. Observe that the value of thename
variable is'
, that is, String.Empty. - You can also see that the value is an empty string by entering the
name
variable name in the Immediate window and pressing enter. - Press ⌘↵ (command+enter) to continue debugging.
- In the terminal window, press any key to exit the program.
- Close the terminal window.
- Clear the breakpoint by clicking on the red dot in the left margin of the code window. Another way to clear a breakpoint is by choosing Run > Toggle Breakpoint while the line of code is selected.
Step through a program
Visual Studio also allows you to step line by line through a program and monitor its execution. Ordinarily, you'd set a breakpoint and follow program flow through a small part of your program code. Since this program is small, you can step through the entire program.
- Set a breakpoint on the curly brace that marks the start of the
Main
method (press command+). - Press ⌘↵ (command+enter) to start debugging.Visual Studio stops on the line with the breakpoint. Time card software for mac.
- Press ⇧⌘I (shift+command+I) or select Run > Step Into to advance one line.Visual Studio highlights and displays an arrow beside the next line of execution.At this point, the Locals window shows that the
args
array is empty, andname
anddate
have default values. In addition, Visual Studio has opened a blank terminal. - Press ⇧⌘I (shift+command+I).Visual Studio highlights the statement that includes the
name
variable assignment. The Locals window shows thatname
isnull
, and the terminal displays the string 'What is your name?'. - Respond to the prompt by entering a string in the console window and pressing enter.
- Press ⇧⌘I (shift+command+I).Visual Studio highlights the statement that includes the
date
variable assignment. The Locals window shows the value returned by the call to the Console.ReadLine method. The terminal displays the string you entered at the prompt. - Press ⇧⌘I (shift+command+I).The Locals window shows the value of the
date
variable after the assignment from the DateTime.Now property. The terminal is unchanged. - Press ⇧⌘I (shift+command+I).Visual Studio calls the Console.WriteLine(String, Object, Object) method. The terminal displays the formatted string.
- Press ⇧⌘U (shift+command+U) or select Run > Step Out.The terminal displays a message and waits for you to press a key.
- Press any key to exit the program.
Use Release build configuration
Once you've tested the Debug version of your application, you should also compile and test the Release version. The Release version incorporates compiler optimizations that can negatively affect the behavior of an application. For example, compiler optimizations that are designed to improve performance can create race conditions in multithreaded applications.
Console App For Visual Studio
To build and test the Release version of the console application, do the following steps:
- Change the build configuration on the toolbar from Debug to Release.
- Press ⌥⌘↵ (option+command+enter) to run without debugging.
Next steps
In this tutorial, you used Visual Studio debugging tools. https://ndenergy555.weebly.com/best-employee-scheduling-software-for-mac.html. In the next tutorial, you publish a deployable version of the app.