C# command line program exit
The following example shows how to use command-line arguments in a console application. The application takes one argument at run time, converts the argument to an integer, and calculates the factorial of the number.
If no arguments are supplied, the application issues a message that explains the correct usage of the program.
Paste the following code into any text editor, and then save the file as a text file with the name Factorial. From the Start screen or Start menu, open a Visual Studio Developer Command Prompt window, and then navigate to the folder that contains the file that you created. If your application has no compilation errors, an executable file that's named Factorial. When running an application in Visual Studio, you can specify command-line arguments in the Debug Page, Project Designer.
For more information, see the C Language Specification. The language specification is the definitive source for C syntax and usage. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Please rate your experience Yes No. Any additional feedback? Note If the examples used async modifier on the Main method, the compiler would generate the same code. Note You can also use Environment. Up Down Thanks, that gives me the return code.
Up Down Hi there, You're welcome - glad it's solved now :. Please log in or register to enter your reply. Knowledge Base Join us Login. What is this? Learn more. Free Download. How to return values in C with exit code? Then name the file Calculator. 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. The Visual Studio Installer launches.
Choose the. NET Core cross-platform development workload, and then choose Modify. In the Create a new project window, choose C 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 select Next. If you don't see the Console Application template, select Install more tools and features.
Then, in the Visual Studio Installer, choose the. 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 Next. In the Additional information window,. NET Core 3. If not, select. Then, choose Create. In the Create a new project window, select All languages , and then choose C from the dropdown list.
Choose Windows from the All platforms list, and choose Console from the All project types list. After you apply the language, platform, and project type filters, choose the Console App template, and then select Next.
If you don't see the Console App template, select Install more tools and features. In the Visual Studio Installer, choose the. NET desktop development workload, and then select Modify. In the Configure your new project window, type or enter Calculator in the Project name box, and then select Next. NET 6. Select Create. Starting with. NET 6, new projects using the console template generate different code than previous versions. To learn more, see the New C templates generate top-level statements page.
Specifically, delete the line that says, Console. WriteLine "Hello World! Notice that when you do so, the IntelliSense feature in Visual Studio offers you the option to autocomplete the entry. If the Main method returns void , you can use this property to set the exit code that will be returned to the calling environment. If Main does not return void , this property is ignored. The initial value of this property is zero.
The ExitCode property is a signed bit integer. To prevent the property from returning a negative exit code, you should not use values greater than or equal to 0x Use a non-zero number to indicate an error. In your application, you can define your own error codes in an enumeration, and return the appropriate error code based on the scenario.
0コメント