Return to site

Visual Studio Console Application

broken image


-->

In this 5-10 minute introduction to the Visual Studio integrated development environment (IDE), you'll create a simple Visual Basic application that runs on the console.

  • Creating a project. Run Visual Studio and select File - New - Project in the application menu. In the New Project window, select the Visual C template - Windows and in the next menu select Empty Project. Let's name this project 'FirstApplication'. Create a folder for your projects in your Dropbox folder, for example, 'cpp/' (as in Plus Plus).
  • This video is for Seneca students to follow for installing Visual Studio 2017 and Creating Simple C and C console apps.Download Visual Studio:https://www.v.
  • When running a console application in Visual Studio via 'Start without Debugging' (Ctrl+F5), the console remains open at the end of the run asking to. Press any key to continue. Thus requiring to activate the window and hit a key. Sometimes this is not appropriate.

Today I created a new.NET Core console application in Visual Studio 2019 preview and added by code without a second thought. When I ran the application I was in for two surprises - the first was the icon had changed, to a fetching purple.

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.

If you haven't already installed Visual Studio 2022 Preview, go to the Visual Studio 2022 Preview downloads page to install it for free.

Create a project

First, you'll create a Visual Basic 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.

  3. In the New Project dialog box in the left pane, expand Visual Basic, and then choose .NET Core. In the middle pane, choose Console App (.NET Core). Then name the project HelloWorld.

    If you don't see the Console App (.NET Core) project template, click 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.

Note

Some of the screenshots in this Quickstart 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.

  1. Open Visual Studio.

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

  3. In the Create a new project window, choose Visual Basic from the Language list. Next, choose Windows from the Platform list and Console from the project types list.

    After you apply the language, platform, and project type filters, choose the Console Application template, and then choose Next.

    Note

    If you do not see the Console 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 .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 WhatIsYourName in the Project name box. Then, choose Next.

  5. In the Additional information window, .NET Core 3.1 should already be selected for your target framework. If not, select .NET Core 3.1. Then, choose Create.

    Visual Studio opens your new project.

Create the application

After you select your Visual Basic project template and name your project, Visual Studio creates a simple 'Hello World' application for you. It calls the WriteLine method to display the literal string 'Hello World!' in the console window.

If you click the HelloWorld button in the IDE, you can run the program in Debug mode.

Visual Studio Console Application

When you do this, the console window is visible for only a moment before it closes. This happens because the Main method terminates after its single statement executes, and so the application ends.

Add some code

Let's add some code to pause the application and then ask for user input.

Visual studio console application template missing
  1. Add the following code immediately after the call to the WriteLine method:

    This pauses the program until you press a key.

  2. On the menu bar, select Build > Build Solution.

    This compiles your program into an intermediate language (IL) that's converted into binary code by a just-in-time (JIT) compiler.

Run the application

  1. Click the HelloWorld button on the toolbar.

  2. Press any key to close the console window.

Next steps

Visual studio console application

Congratulations on completing this Quickstart! We hope you learned a little bit about Visual Basic and the Visual Studio IDE. To learn more, continue with the following tutorial.

C# is one of the languages provided by Microsoft to work with .Net. This language encompasses a rich set of features, which allows developing different types of applications.

C# is an object-oriented programming language and resembles several aspects of the C++ Language. In this tutorial, we see how to develop our first application.

This will be a basic console application, we will then explore different data types available in the C# language as well as the control flow statements.

Visual Studio Console Application Pause

Building the first console application

A console application is an application that can be run in the command prompt in Windows. For any beginner on .Net, building a console application is ideally the first step to begin with.

In our example, we are going to use Visual Studio to create a console type project. Next, we are going to use the console application to display a message 'Hello World'. We will then see how to build and run the console application.

Let's follow the below mentioned steps to get this example in place.

Step 1) The first step involves the creation of a new project in Visual Studio. For that, once the Visual Studio is launched, you need to choose the menu option New->Project.

Step 2) The next step is to choose the project type as a Console application. Here, we also need to mention the name and location of our project.

Visual Studio Console Application Output

  1. In the project dialog box, we can see various options for creating different types of projects in Visual Studio. Click the Windows option on the left-hand side.
  2. When we click the Windows options in the previous step, we will be able to see an option for Console Application. Click this option.
  3. We then give a name for the application which in our case is DemoApplication. We also need to provide a location to store our application.
  4. Finally, we click the 'OK' button to let Visual Studio to create our project.

If the above steps are followed, you will get the below output in Visual Studio.

Output:-

  1. A project called 'DemoApplication' will be created in Visual Studio. This project will contain all the necessary artifacts required to run the Console application.
  2. The Main program called Program.cs is default code file which is created when a new application is created in Visual Studio. This code will contain the necessary code for our console application.

Step 3) Now let's write our code which will be used to display the string 'Hello World' in the console application.

Visual Studio Console Application Vs Windows Application

All the below code needs to be entered into the Program.cs file. The code will be used to write 'Hello World' when the console application runs.

C# Hello World Program

Code Explanation:-

  1. The first lines of code are default lines entered by Visual Studio. The 'using' statement is used to import existing .Net modules in our console application. These modules are required for any .Net application to run properly. They contain the bare minimum code to make a code work on a Windows machine.
  2. Every application belongs to a class. C# is an object-oriented language, and hence, all code needs to be defined in a self-sustaining module called a 'Class.' In turn, every class belongs to a namespace. A namespace is just a logical grouping of classes.
  3. The Main function is a special function which is automatically called when a console application runs. Here you need to ensure to enter the code required to display the required string in the console application.
  4. The Console class is available in .Net which allows one to work with console applications. Here we are using an inbuilt method called 'Write' to write the string 'Hello World' in the console.
  5. We then use the Console.ReadKey() method to read any key from the console. By entering this line of code, the program will wait and not exit immediately. The program will wait for the user to enter any key before finally exiting. If you don't include this statement in code, the program will exit as soon as it is run.

Step 4) Run your .Net program. To run any program, you need to click the Start button in Visual Studio.

If the above code is entered properly and the program is executed successfully, the following output will be displayed.

Output:

Visual studio console application pause
  1. Add the following code immediately after the call to the WriteLine method:

    This pauses the program until you press a key.

  2. On the menu bar, select Build > Build Solution.

    This compiles your program into an intermediate language (IL) that's converted into binary code by a just-in-time (JIT) compiler.

Run the application

  1. Click the HelloWorld button on the toolbar.

  2. Press any key to close the console window.

Next steps

Congratulations on completing this Quickstart! We hope you learned a little bit about Visual Basic and the Visual Studio IDE. To learn more, continue with the following tutorial.

C# is one of the languages provided by Microsoft to work with .Net. This language encompasses a rich set of features, which allows developing different types of applications.

C# is an object-oriented programming language and resembles several aspects of the C++ Language. In this tutorial, we see how to develop our first application.

This will be a basic console application, we will then explore different data types available in the C# language as well as the control flow statements.

Visual Studio Console Application Pause

Building the first console application

A console application is an application that can be run in the command prompt in Windows. For any beginner on .Net, building a console application is ideally the first step to begin with.

In our example, we are going to use Visual Studio to create a console type project. Next, we are going to use the console application to display a message 'Hello World'. We will then see how to build and run the console application.

Let's follow the below mentioned steps to get this example in place.

Step 1) The first step involves the creation of a new project in Visual Studio. For that, once the Visual Studio is launched, you need to choose the menu option New->Project.

Step 2) The next step is to choose the project type as a Console application. Here, we also need to mention the name and location of our project.

Visual Studio Console Application Output

  1. In the project dialog box, we can see various options for creating different types of projects in Visual Studio. Click the Windows option on the left-hand side.
  2. When we click the Windows options in the previous step, we will be able to see an option for Console Application. Click this option.
  3. We then give a name for the application which in our case is DemoApplication. We also need to provide a location to store our application.
  4. Finally, we click the 'OK' button to let Visual Studio to create our project.

If the above steps are followed, you will get the below output in Visual Studio.

Output:-

  1. A project called 'DemoApplication' will be created in Visual Studio. This project will contain all the necessary artifacts required to run the Console application.
  2. The Main program called Program.cs is default code file which is created when a new application is created in Visual Studio. This code will contain the necessary code for our console application.

Step 3) Now let's write our code which will be used to display the string 'Hello World' in the console application.

Visual Studio Console Application Vs Windows Application

All the below code needs to be entered into the Program.cs file. The code will be used to write 'Hello World' when the console application runs.

C# Hello World Program

Code Explanation:-

  1. The first lines of code are default lines entered by Visual Studio. The 'using' statement is used to import existing .Net modules in our console application. These modules are required for any .Net application to run properly. They contain the bare minimum code to make a code work on a Windows machine.
  2. Every application belongs to a class. C# is an object-oriented language, and hence, all code needs to be defined in a self-sustaining module called a 'Class.' In turn, every class belongs to a namespace. A namespace is just a logical grouping of classes.
  3. The Main function is a special function which is automatically called when a console application runs. Here you need to ensure to enter the code required to display the required string in the console application.
  4. The Console class is available in .Net which allows one to work with console applications. Here we are using an inbuilt method called 'Write' to write the string 'Hello World' in the console.
  5. We then use the Console.ReadKey() method to read any key from the console. By entering this line of code, the program will wait and not exit immediately. The program will wait for the user to enter any key before finally exiting. If you don't include this statement in code, the program will exit as soon as it is run.

Step 4) Run your .Net program. To run any program, you need to click the Start button in Visual Studio.

If the above code is entered properly and the program is executed successfully, the following output will be displayed.

Output:

From the output, you can clearly see that the string 'Hello World' is displayed properly. This is because of the Console.write statement causes this string to be sent to the console.

Summary

  • A Console application is one that can be made to run at the command prompt on a windows machine.
  • The Console.write method can be used to write content to the console.




broken image