Visual Studio 2017 C Application Dev Tutorial

  1. Visual Studio 2017 C Application Dev Tutorial For Beginners
  2. Visual Dev Studio
  3. Visual Studio 2017 C Application Dev Tutorial Youtube
-->

Nov 26, 2018 Visual Studio isn’t just for Windows C and C development anymore. If you follow the tutorial below on your own machine, you will clone an open source project from GitHub, open it in Visual Studio, edit, build and debug on Windows with no changes to the project. Get everything you need to build and deploy your app on any platform. With state-of-the-art tools, the power of the cloud, training, and support, it’s our.

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.

  1. Visual Studio 2017 Installation. In Visual Studio 2017, it's easy to choose and install just the features you need. And because of its reduced minimum footprint, it installs quickly and with less system impact. A broadband internet connection. The Visual Studio installer can download several gigabytes of data.
  2. Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications. Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows.
  3. Apr 11, 2017  DirectX game development with C in Visual Studio. Allows you to edit your C code during a debugging session and see the impact right away without having to rebuild the application. Try out Visual Studio 2017.

Visual Studio 2017 C Application Dev Tutorial For Beginners

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!

  1. Open Visual Studio 2017.

  2. From the top menu bar, choose File > New > Project.(Alternatively, press Ctrl+Shift+N).

  3. 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.

Visual

Option 1: Use the New Project dialog box

  1. Choose the Open Visual Studio Installer link in the left pane of the New Project dialog box.

  2. The Visual Studio Installer launches. Choose the .NET Core cross-platform development workload, and then choose Modify.

Option 2: Use the Tools menu bar

  1. Cancel out of the New Project dialog box and from the top menu bar, choose Tools > Get Tools and Features.

  2. The Visual Studio Installer launches. Choose the .NET Core cross-platform development workload, and then choose Modify.

  1. Open Visual Studio 2019.

  2. On the start window, choose Create a new project.

  3. 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.

    Note

    If 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.

  4. 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#.

  1. In the code editor, delete the default 'Hello World' code.

    Specifically, delete the line that says, Console.WriteLine('Hello World!');.

  2. 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.

    Note

    The following animation isn't intended to duplicate the preceding code. It's intended only to show how the autocomplete feature works.

  3. 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.

  4. (Optional) You can change the operator to change the result. For example, you can change the + operator in the int c = a + b; line of code to - for subtraction, * for multiplication, or / for division. Then, when you run the program, the result changes, too.

  5. 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.

  1. Delete all the code you see in the code editor.

  2. Enter or paste the following new code into the code editor:

  3. Choose Calculator to run your program, or press F5.

    A console window opens.

  4. 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.

  1. Press Ctrl + F to open the Find and Replace control.

  2. Change each instance of the int variable to float.

    Make sure that you toggle Match case (Alt+C) and Match whole word (Alt+W) in the Find and Replace control.

  3. 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.

  1. Use the Find and Replace control (Ctrl + F) to change each instance of the float variable to double, and to change each instance of the Convert.ToInt32 method to Convert.ToDouble.

  2. 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

Visual Dev Studio

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.

  1. Delete the code that appears directly between case 'd': and the comment that says // Wait for the user to respond before closing.

  2. 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.

Visual Studio 2017 C Application Dev Tutorial Youtube

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.

  1. Delete everything in the Calculator namespace between its opening and closing braces:

  2. Next, add a new Calculator class, as follows:

  3. Then, add a new Program class, as follows:

  4. Choose Calculator to run your program, or press F5.

  5. 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

  1. If you haven't already done so, close the calculator app.

  2. Close the Output pane in Visual Studio.

  3. In Visual Studio, press Ctrl+S to save your app.

  4. 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

-->

In this 5-10 minute introduction to how to use Visual Studio, you'll create a simple 'Hello World' web app by using an ASP.NET project template and the C# programming language.

Before you begin

Install Visual Studio

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.

Choose your theme (optional)

This quickstart tutorial includes screenshots that use the dark theme. If you aren't using the dark theme but would like to, see the Personalize the Visual Studio IDE and Editor page to learn how.

Create a project

To start, you'll create an ASP.NET Core web application project. The project type comes with all template files to create a web app, before you've even added anything!

  1. Open Visual Studio 2017.

  2. From the top menu bar, choose File > New > Project.

  3. In the left pane of the New Project dialog box, expand Visual C#, and then choose .NET Core. In the middle pane, choose ASP.NET Core Web Application.
    Then, name your file HelloWorld and choose OK.

    Note

    If you don't see the .NET Core project template category, choose the Open Visual Studio Installer link in the left pane. (Depending on your display settings, you might have to scroll to see it.)

    The Visual Studio Installer launches. Choose the ASP.NET and web development workload, and then choose Modify.

    (You might have to close Visual Studio before you can continue installing the new workload.)

  4. In the New ASP.NET Core Web Application dialog box, select ASP.NET Core 2.1 from the top drop-down menu. Next, choose Web Application, and then choose OK.

    Note

    If you don't see ASP.NET Core 2.1, make sure that you are running the most recent release of Visual Studio. For more information about how to update your installation, see the Update Visual Studio to the most recent release page.

Soon after, Visual Studio opens your project file.

  1. Open Visual Studio.

  2. On the start window, choose Create a new project.

  3. On the Create a new project window, enter or type ASP.NET 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 ASP.NET Core Web Application template, and then choose Next.

    Note

    If you do not see the ASP.NET Core Web Application 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 ASP.NET and web 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.

  4. In the Configure your new project window, type or enter HelloWorld in the Project name box. Then, choose Create.

  5. In the Create a new ASP.NET Core Web Application window, verify that ASP.NET Core 3.0 appears in the top drop-down menu. Then, choose Web Application, which includes example Razor Pages. Next, choose Create.

    Visual Studio opens your new project.

Create and run the app

  1. In the Solution Explorer, expand the Pages folder, and then choose About.cshtml.

    This file corresponds to a page that's named About in the web app, which runs in a web browser.

    In the editor, you'll see HTML code for the 'additional information' area of the About page.

  2. Change the 'additional information' text to read 'Hello World!'.

  3. In the Solution Explorer, expand About.cshtml, and then choose About.cshtml.cs. (This file also corresponds with the About page in a web browser.)

    In the editor, you'll see C# code that includes text for the 'application description' area of the About page.

  4. Change the 'application description' message text to read 'What's my message?'.

  5. Choose IIS Express or press Ctrl+F5 to run the app and open it in a web browser.

    Note

    If you get an error message that says, Unable to connect to web server 'IIS Express', or an error message that mentions an SSL certificate, close Visual Studio. Next, open Visual Studio by using the Run as administrator option from the right-click context menu. Then, run the application again.

  6. In the web browser, verify that the About page includes your updated text.

  7. Close the web browser.

Review your work

View the following animation to check the work that you completed in the previous section.

Congratulations on completing this Quickstart! We hope you learned a little bit about C#, ASP.NET Core, and the Visual Studio IDE (integrated development environment).

  1. In the Solution Explorer, expand the Pages folder, and then choose Index.cshtml.

    This file corresponds to a page that's named Home in the web app, which runs in a web browser.

    In the editor, you'll see HTML code for the text that appears on the Home page.

  2. Change the 'Welcome' text to read 'Hello World!'.

  3. Choose IIS Express or press Ctrl+F5 to run the app and open it in a web browser.

    Note

    If you get an error message that says, Unable to connect to web server 'IIS Express', or an error message that mentions an SSL certificate, close Visual Studio. Next, open Visual Studio by using the Run as administrator option from the right-click context menu. Then, run the application again.

  4. In the web browser, verify that the Home page includes your updated text.

  5. Close the web browser.

Next steps

To learn more, continue with the following tutorial:

See also