日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 数组

發布時間:2025/3/18 C# 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 数组 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C# 數組

二維數組由若干個一維數組組成。

在C++中,組成二維數組的一維數組長度必須相等。在C#中卻可以不相等。

C#二維數組有兩種:

1,普通二維數組:

int [,] arr2d = new int[3,2]; int[,] scroes2d2 = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };

2,數組的數組:又稱鋸齒數組,交錯數組

int[][] varr = new int[3][] //不能寫成int[][] varr = new int[3][2] int[][] varr2 = new int[3][] { new int[1] { 1 }, new int[3] { 1, 2, 3 }, new int[2] { 1, 2 } };

測試代碼:

class Program{static void Main(string[] args){int[] scroes = new int[5];int[,] scroes2d = new int[3,2];Console.WriteLine("scroes2d.length:{0}", scroes2d.Length); //6 for(int i=0; i<3; ++i){for(int j=0; j<2; ++j){scroes2d[i, j] = i * 2 + j;}}int[][] varr = new int[3][];Console.WriteLine("varr.length:{0}", varr.Length); //3 for(int i=0; i<varr.Length; ++i){varr[i] = new int[i + 1];for(int j=0; j<varr[i].Length; ++j){varr[i][j] = j;}}foreach(var arr in varr){foreach(var n in arr)Console.Write(n);Console.WriteLine();}Console.ReadKey();}}

?

  • 附:參數數組

有時,當聲明一個方法時,您不能確定要傳遞給函數作為參數的參數數目。C# 參數數組解決了這個問題,參數數組通常用于傳遞未知數量的參數給函數。

在使用數組作為形參時,C# 提供了 params 關鍵字,使調用數組為形參的方法時,既可以傳遞數組實參,也可以只傳遞一組數組。params 的使用格式為:

public 返回類型 方法名稱( params 類型名稱[] 數組名稱 )

??? class CTest
??? {
??????? public void func(params int[] arr)
??????? {
??????????? foreach(var n in arr)
??????????? {
??????????????? Console.Write(n + ",");
??????????? }
??????? }
??? }
??? class Program
??? {
??????? static void Main(string[] args)
??????? {

???????????   CTest ot = new CTest();
???????????   ot.func(1,2,3,4,5,6,7,8,9,0);
???????????   ot.func(new int[] { 1, 2, 3, 4 });

     }
??????? }

?

posted on 2016-10-01 17:29 時空觀察者9號 閱讀(...) 評論(...) 編輯 收藏

總結

以上是生活随笔為你收集整理的C# 数组的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。