Visual Studio Console Application

  1. Visual Studio Console Application Args
  2. Visual Studio What Is A Console Application

New option in Visual Studio 2019 keeps console window open after debugging

For as long as I've been writing console applications, I've atthe end of Main I've always had a piece of code to requireuser input before exiting when in debug mode, in order to reviewany output before the window closes. Not an onerous task, butone I immediately do as soon as creating a new application.

Today I created a new .NET Core console application in VisualStudio 2019 preview and added by code without a second thought.

When I ran the application I was in for two surprises - thefirst was the icon had changed, to a fetching purple. The secondwas, after the program had ran and displayed my usual '(Pressany key to exit.)' message, the console stayed open - withanother message to press a key to exit.

A small change, but a welcome one! And, just in case it's not toeveryone's liking, there's even an option to disable thebehaviour if you don't like it.

Open Visual Studio. On the start window, choose Create a new project. 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.

Always a catch

  • Visual Studio enables nice features where you can do this on the Debug tab. Here are the steps to achieve this. Right Click on Project from Solution Explorer and Select Properties. In the Project Properties Windows, Navigate to “Debug Tab”. You will find the text box “Command Line”. Well, here you can type the command line.
  • Open visual studio 2015/2017/2019 and then File - New - Project. Here from the left side, choose Templates - Visual C#. And then from the list of template choose Console Application. Make sure to choose the.net framework version to “.NET Framework 4.5.2 ” like below: Now our console application is ready, but to work with SharePoint.

Unfortunately, this new option doesn't seem to work for oldschool console applications using the full .NET Framework, soI'll continue adding my boilerplate blocks to any new .NETFramework console applications I create.

  • 2019-03-02 - First published
  • 2020-11-22 - Updated formatting

While we appreciate comments from our users, please follow our posting guidelines. Have you tried the Cyotek Forums for support from Cyotek and the community?

larry andrews

This is quite messed up for console apps. The checkbox does nothing for a run under the debugger. It closes the output window whatever the checkbox is set to. This is the old behavior for debug runs. HOWEVER, for non-debug running of debug compilations, the new default if that the box is unchecked and the window closes immediately. Setting the checkbox (automatically close the console when debugging stops) changes the non-debug run to leave the console window open at the end of execution. Totally confusing and mislabeled. WORSE: if you are running Visual Studio 2017 also, the options apply there also, so you suddenly lose the output window in non-debug runs.

Mix-Movie.com

I had the same problem, when I created projects with the option 'empty project', Create project as 'Win32-console application' instead of 'empty project'. In the dialog which pops up now, you press 'continue' and after that you may check the option 'empty project' and press confirm. After that CTRL + F5 will open a console which does not close automatically.

Alex

You could use this extension for redirection stdout into Visual Studio output window https://marketplace.visualstudio.com/items?itemName=NesviatypaskhaOleksii.VsConsoleOutput

Just click the button below to start the whole online course!

Or use Facebook:

By registering you agree with our terms of use. You can read how we process your data.

Already have an account?

Lesson highlights

Are you looking for a quick reference on creating a C# project in Visual Studio instead of a thorough-full lesson? Here it is:

  • Download Visual Studio Community Edition at https://www.visualstudio.com/…nity-vs.aspx
  • Install Visual Studio and check to install .NET desktop development kit, choose C# if needed
  • Run Visual Studio and select File -> New -> Project in the application menu:
  • Select the Visual C# template -> Windows and in the next menu select Console Application:
  • Edit the project name and location if needed and confirm the form
  • Hit the green play button in the toolbar to run the project.

Would you like to learn more? A complete lesson on this topic follows.

In the previous lesson, Introduction to C# and the .NET framework, we talked about the language itself andwent over what the .NET framework is. In today's lesson, we're going to focus onthe Visual Studio IDE. We'll show you how to use it and program a simple consoleapplication.

IDE stands for Integrated Development Environment. In a nutshell, it's anapplication in which we'll write a source code, and then use it to run, test anddebug our application.

Of course, we'll have to start by installing Visual Studio. If you haveaccess to paid versions, which come with advanced features, go ahead and use it,but the free Community version will be more than enough for us. You can downloadthe Visual Studio 2015 Community Edition on the officialMicrosoft site.

Installation

Even if English isn't your native language, I highly suggest installing theEnglish version of Visual Studio. The advantage to using English applications isthat if you run into problems, you will most likely not be able to find theanswers in your language (e.g. with the advanced configuration of databases orwebsites). All of your applications and programs should be also written inEnglish, but for the start feel free to use your native language without usingaccent characters, if your language has any. If you want to become aprofessional programmer, you should try to improve your language skills, andwrite everything in English.

The installation will ask about the preferred language, choose C# andcontinue by clicking the next button . After installation, you may need to register your Visual Studio.Registration is free, after which you'll receive a serial number that allows youto use this program legally.

Backup and version control

Programmers usually need a tool that will provide version control and backupof his work. We can't rely on the fact that we could just save the programbecause we're humans and humans make mistakes. When you lose a few days' or evena few weeks' work, it can be really demotivating. It's good to think about suchsituations right from the start. I highly recommend Dropbox, which is extremelysimple, and automatically stores multiple versions of yourfiles, which makes it possible to revert to previous versions of the project,and also synchronizes with a web repository. Even if you'veaccidentally deleted your project, overwrote it, somebody stole your laptop oryour hard drive accidentally collapsed, your data will remain safe. Dropbox alsoallows you to share projects with several developers.

You can also use a tool called GIT for the same purposes, but itsconfiguration would require the 'while' article. Dropbox is perfect for ourcurrent intents and purposes.

Creating a project

Run VS and select File -> New -> Project in the application menu.

In the New Project window, select the Visual C# template -> Windows and inthe next menu select Console Application. Let's name this projectFirstApplication. Then, switch the target framework to the .NETFramework 3.5 if you want to support Windows 7 users without additionalframework installation (for Windows 8 and above choose .NET 4.5). Create afolder for your projects in your Dropbox folder, for example,csharp. Use the Browse button and select a folderC:Usersyour_nameDropboxcsharp. We will stick with consoleapplications, command line interface, for a while because it needs minimalknowledge of the object-oriented world, and they are ideal for learning thebasics of the language. The window should look like this:

Confirm the form.

Visual Studio Tutorial

The window now looks like this (I have resized it to fit ):

We're interested mainly in the middle window in which VS will generate somesource code. It may be a surprise to some of you that we are not starting outwith an empty window, but then again that is exactly what a template is (easilymodifiable preset code)! We'll keep it simple for now, everything will beexplained throughout the courses. Some parts are quite advanced, so we'll justaccept the fact they're there and go into detail later.

The first few lines are telling us what libraries from .NET we are using. Themost important one for us is System. Without it, we could hardlyprogram anything, because it contains basic methods for working with theconsole. We won't pay any attention to namespace andclass, just keep in mind that C# uses them to structure programs.The key will be the Main() method for us, we'll write our codebetween the curly brackets below it, i.e. the body. Main is areserved word, and C# knows that this method must be executed as first after theapplication starts (there are more methods, but more on that later). Actually,we'll ignore everything except for the Main() method.

The second important element in the window is a green 'Play' button in theupper bar, which compiles and runs our program. You can try it, but our programwill not do anything, just turn terminate right away. You can also run theprogram with the F5 keyboard shortcut. Shortcuts in VS are designedvery well and it will simplify your work a lot, that is if you decide to learnthem.

Near the arrow icon, we have Debug selected. This means that the program willbe compiled in the Debug mode, and it'll contain certain routines to debugerrors. It's mainly used to test the program when we're developing it, and theprogram may run slower due to it. Once you're sure that your program iscomplete, switch it to Release mode and run the program. As a result, we'll havea program which can be distributed among people.

Console

Directory structure of the console application

Let's take a look at what's in our application folder. Open it up, mine's inC:Usersyour_nameDropboxcsharpFirstApplication, and find afile named FirstApplication.sln. Sln stands for 'Visual Studio solution '. It's a set of projects and it maycontain multiple applications which are used for multi-tier applications, or fortesting. We'll open our applications using this file. There should also be afolder named FirstApplication containing our project. Let's openit up, and see what's inside.

The FirstApplication.csproj file contains a project file that wecan use to open up our app. Program.cs contains the actual sourcecode. We are also interested in the bin/ folder,whose name suggests that it contains binary (machine) code of ourapplication.

Inside the bin folder, you'll see that it contains Debug/ andRelease/ subfolders. There are .exe files of ourapplication inside of it (that is if we run the application at leastonce in the corresponding mode). In order to show your application to somebody,this .exe file in the Release/ subfolder is the file you would haveto send him or her. Don't worry about any of the other files, for now.

Hello world

Visual Studio Console Application Args

As tradition instructs, we'll make the first program most people make whenthey learn a new language -Hello World. This is a program that displays 'Helloworld' or some similar text. Again, let me remind you that we'll write commandsin the body of the Main() method. We'll need two commands (note:I'm using the word command just for simplification): first one to display thetext and the second one to wait for the user to press any key to prevent theprogram from terminating immediately.

To write text we use the following bit of code:

Then, we wait for the user to press a key:

Console is a Class. For now, we'll see classesas sets of available commands. In C#, commands are called methods. Soconsole includes methods for console operations. We call theWriteLine() method from it that displays a text. You can see thatwe use a dot operator (.) to call the method in the class. Methodscan receive input parameters, which are entered in parentheses and separated bycommas. In the case of the WriteLine() method, the parameter is thetext to be printed. In programming, texts are called strings, like a string ofcharacters, and written it in quotation marks. C# sees the quotation marks andinterprets it as text. The ReadKey() method has no parameters, butwe still have to add parentheses after the method name because it is C# needs itto be this way. We should write the commands on separate lines, and after everyline, we should put a semicolon (;). Our Main() methodwill now look like this:

You can run the program by pressing F5 if your system hasn'tassigned a different command for the key. You can also run code snippets fromour articles by pressing the 'Play' button above, you can edit the code as well,just click on it. Try that This makes you able to study from anywhere, even without your IDE.

Congratulations, you have just become a programmer That will be all for today, inthe next lesson, Variables, type system and parsing in C# .NET, we'll look at the basic data types and create a simplecalculator.

Today's project is attached as a file at the end of the article. You couldalways download the result below each lesson. I suggest that you create aproject using the tutorial, and if something in your project doesn't work, thendownload it to find a mistake. If you download the lesson code before eventrying to make it yourself, you won't learn anything


Did you have a problem with anything? Download the sample application below and compare it with your project, you will find the error easily.

Download

By downloading the following file, you agree to thelicense terms

Downloaded 75x (32.6 kB)
Application includes source codes in language C#

Do you like this article?
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.

Visual Studio What Is A Console Application

David learned IT at the Unicorn University - a prestigious college providing education on IT and economics.