C# Tutorial - Application 2 – Hello ParameterCreate a new console Application called App02. We are going to change the code to make this application interactive so that instead of printing Hello World! the application prints Hello yourname, where you enter your name as a parameter when executing the application. The full listing for the application is given below. using System;
using System.Collections.Generic;
using System.Text;
namespace App02
{
class Program
{
static void Main(string[] args)
{
foreach (string s in args)
Console.WriteLine("Hello " + s);
Console.Read();
}
}
}
This application takes a parameter or parameters from the command line and outputs them as part of the string that is displayed in the Console window. In order to add command line parameters to the project, we need to open the properties of the project. This can be done by either double-clicking on the Properties folder in the Solution Explorer, or by choosing App02 Properties from the Project menu. This will open the "Project Properties" dialog on a new tab in the environment. Click on the Debug tab on the left hand side of the page. This will display the "Start" options ![]() Enter your name in the Command line arguments text box and click on OK. When you run the application, your name will be displayed after "Hello". You can place as many command line arguments as you wish using white space to separate each argument. When you save a project, the Project Properties are also saved. Open a command prompt by clicking on the start menu and then on the entry marked Run.... When you have done this, a dialog box will appear. In the text type cmd and click on OK. A dialog box will appear. Navigate to your code directory by using the Change Directory command. CD.. will take you up one folder level from the current position, while CD folder_name will change you to the name of the folder in the current directory. To change drive letters type CD C:, where C is the letter of the drive that you wish to change to. When you have reached the required folder, type App02 Bob, where Bob is your name. Close the command window by typing Exit.
|