Home > Articles > Programming > C#

  • Print
  • + Share This
From the author of

From the author of

Interacting with Programs

One of the things done with many programs is user interaction at runtime. You can do this on the command line. This knowledge may be useful if there's a need to create a command-line utility. Listing 2 shows an example of interacting with a program via the command line.

Listing 2: An Interactive Program

 1: /*
 2:  Filename: HelloName.cs
 3:  Author:  Joe Mayo
 4: */
 6: using System;  // use the System namespace
 8: class HelloName
10:   static void Main()
11:   {
12:     string name;  // holds user's name
14:     // Ask for input
15:     Console.Write("What is your name?: ");
17:     // Get user's input
18:     name = Console.ReadLine();
20:     // Print greeting to console
21:     Console.WriteLine("Hello, {0}! ", name);
22:   }
23: }

One of the first differences from Listing 1 in Listing 2 is the using System; statement on Line 6. This indicates that class and method names in this file can be written without putting the namespace name System in front. On Lines 15, 18, and 21, System is not in front of these statements anymore. The class name is changed to HelloName because it provides a better idea of the purpose of the class. Its starting method is Main(), and this name will never change.

Within the Main() method is the first variable on Line 11. It has an identifier called name, and it is a string type.

Line 15 prints a message to the screen, asking the user for input. This particular command, Console.Write(), differs from Console.WriteLine() in that it prints the message to the screen and leaves the carat on the same line. In this case, it produces the desired effect of having the user type on the same line as the question being asked.

The Console.ReadLine() statement on Line 18 causes the system to wait for the user to type some series of characters and press the Enter or Return key. Pressing the Enter or Return key causes the system to generate a newline character. The series of characters can be any valid string except for the newline. Once a newline is encountered, the ReadLine() method will return all the characters entered on the command line except for the newline. The assignment operator, =, puts that string of characters into the name variable.

On Line 21, the string that the user entered displays on the screen with the Console.WriteLine() method. Both the Console.Write() and Console.WriteLine() methods allow formatting of strings. The Console.WriteLine() method on Line 21 of Listing 2 takes a single parameter, {0}, inside its string argument, "Hello, {0}!". The second argument in Console.WriteLine() is the string variable name. When the program executes this statement, it replaces the parameter {0} with the value of the variable name. Figure 3 shows the output of this program.

Figure 3 Output from Listing 2.

Programs can run with command-line parameters. This is useful when implementing a certain configuration with a script or desktop shortcut. Listing 3 shows an example of accepting command-line arguments.

Listing 3: Accepting Command-Line Arguments

 1: /*
 2:  Filename:  HelloCmdLine.cs
 3:  Author:   Joe Mayo
 4: */
 6: using System;
 8: // Program start class
 9: class HelloCmdLine
10: {
11:    // Accept command line arguments
12:    public static void Main(string[] args)
13:    {
14:     // Write to console
15:     Console.WriteLine("Hello, {0}!", args[0]);
16:    }
17: }

The first difference from Listing 2 is on Line 12, where the Main() method has a parameter. This parameter is an array type named args that can hold a list of string types. The system populates this array from the entries added in the command line following the program name.

On Line 15 is a Console.WriteLine() statement, similar to the one in Listing 2. It accepts an argument of args[0]. This argument, args[0], holds the first element of the args array. Subsequent arguments would be in args[1], args[2], ..., args[n]. This program replaces the {0} parameter with the value of args[0] when it prints to the console. Figure 4 shows how to use this program.

Figure 4 Demonstration of command-line input for Listing 3.

For C++ Programmers

C++ command-line entries are passed with two variables to main, argc (a count of variables) and argv (an array of pointers to strings holding command-line input). This is accomplished in C# with a single array variable, args, with built-in functions to determine the number of strings in the array.

  • + Share This
  • 🔖 Save To Your Account