首頁  >  文章  >  後端開發  >  SUNWEN教程之----C#進階(二)

SUNWEN教程之----C#進階(二)

黄舟
黄舟原創
2016-12-19 10:16:121021瀏覽

好了,I COME BACK,下面的程式碼顯示了我們如何列印多個參數:

000: // CommandLinecmdline1.cs
001: using System;
002:
003: public class CommandLine005 : public static void Main(string[] args)
006: {
007: Console.WriteLine("Number of command line parameters = {0}", args.Length);
008: for(int i = 0 i(int i = i = 0; 009: {
010: Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
011: }
012: }
013: }
如果你的輸入是:

cmdline1 A B C

那麼它的輸出為:

Number of command line parameters = 3

Arg[0] = [A]
Arg[1] = [ArgB]
Arg[0] = [A]
Arg[1] = [ArgB]
Arg [2] = [C]

哈哈,看出了其中的秘密了吧!是的{0}是一個標記,它告訴系統,這裡留下了給第0個參數用,在程序中,這第0個參數就是i.因此,印出來的就是Arg[0],面不是Arg[{0}]了,哈哈!

例二向大家展示了foreach這個語句的用法,其實在asp中,這個經常用到:


000: // CommandLinecmdline2.cs
001: using System;
002: 
003: public class CommandLine2
004: {
005: public class CommandLine2
004: {
005: public class 007: Console.WriteLine("Number of command line parameters = {0}", args.Length);
008: foreach(string s in args)
009: {
010: Console.WriteLine(s); }
012: }
013: }
很顯然,args是一個數組,而且是字符型的.foreach的意思就是把其中的所有元素都循環完.運行得出:

>cmdline2 John Paul Mary

Number of command line parameters = 3
John
Paul
Mary
好了,同志們,休息一下,我要睡覺了!88,下次再見!

 以上就是SUNWEN教程之----C#進階(二)的內容,更多相關內容請關注PHP中文網(www.php.cn)!



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn