C # advanced tutorial (ii) print multiple parameters

Source: Internet
Author: User

Okay, I COME BACK. The following code shows how to print multiple parameters:

000: // CommandLinecmdline1.cs
001: using System;
002:
003: public class CommandLine
004 :{
005: public static void Main (string [] args)
006 :{
007: Console. WriteLine ("Number of command line parameters = {0}", args. Length );
008: for (int I = 0; I <args. Length; I ++)
009 :{
010: Console. WriteLine ("Arg [{0}] = [{1}]", I, args [I]);
011 :}
012 :}
013 :}
If your input is:

Cmdline1 A B C

The output is:
Number of command line parameters = 3
Arg [0] = [A]
Arg [1] = [B]
Arg [2] = [C]

Haha, I saw the secret! Yes {0} is a flag. It tells the system that 0th parameters are left for use. In the Program, The 0th parameters are I. therefore, the output is Arg [0], and the surface is not Arg [{0}]. Haha!

Example 2 shows the usage of the foreach statement. In ASP, this statement is often used:


000: // CommandLinecmdline2.cs
001: using System;
002:
003: public class CommandLine2
004 :{
005: public static void Main (string [] args)
006 :{
007: Console. WriteLine ("Number of command line parameters = {0}", args. Length );
008: foreach (string s in args)
009 :{
010: Console. WriteLine (s );
011 :}
012 :}
013 :}
Obviously, args is an array, and it is nested. foreach means that all the elements in it are cyclically completed. Run the following code:

> Mongoline2 John Paul Mary
Number of command line parameters = 3
John
Paul
Mary
Okay, Comrades. Have a rest. I'm going to bed! 88. Goodbye next time!

Related Article

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.