C # Concise Tutorial II

Source: Internet
Author: User
Tutorial section II: Reading command line input

Many programs can accept command-line input, and the following procedure is to accept the command line's loss
And then print it to the console screen.


Program 1-2:namedwelcome.cs

Namespace Declaration

Using System;


Program Start class

Class Namedwelcome {


Main begins program execution.

public static void Main (string[] args) {


Write to Console

Console.WriteLine ("Hello, {0}!", args[0]);

Console.WriteLine ("Welcome to the" C # station tutorial!);


}

}


Compile the program to NamedWelcome.exe and lose at the command line
into "Namedwelcome Joe". Be sure to add a name or something to the program.
Otherwise, the program will go wrong. Later we will discuss how to detect and avoid such errors.


In program 1-2, you can see that the argument list for the "Main" method has a parameter
Number, the name is "args", which is the parameter that the following program will use. "String[]"
The function is to define the parameter's data type as a string type and can hold one or more characters.
The square bracket "[]" means "string[]" is an array that can hold multiple parameters. Because
This, the parameter can be one or more arguments from the command line.


You will notice the Console.WriteLine ("Hello, {0}!", args[0]);
Line program. This line of procedures differs from the previous one, with an extra parameter ' {0} ' inside the quotation marks.
In a WriteLine statement, a parameter is a way to represent a method, and the format of the first argument
Is ' {0} ', the second is ' {1} ', and so on. Here "{0}" is represented by the end primer
The parameter "Args[0]" after the number to determine the contents of "{0}". According to this idea, we
Take a look at the argument "args[0]" after the closing quote content.

  


The "args[0]" argument refers to the first element in the "args" array, and the second element
Is "args[1]", and so on. For example, I lose in command line mode
The value of "Namedwelcome Joe", "Args[0]" is "Joe."


Now let's look at the Code: Console.WriteLine ("Hello, {0}!",
Args[0]); , "{0}" will be replaced by the value in the parameter "args[0]" during execution,
Output to the screen along with the "Hello," front. So when we execute the program, we lose.
When you enter "Namedwelcome Joe", the following will be output:


>hello, joe!

>welcome to the C # station tutorial!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.