版權聲明:本文為博主原創文章,轉載請在顯著位置標明本文出處以及作者網名,未經作者允許不得用于商業目的
版權聲明:本文為博主原創文章,轉載請在顯著位置標明本文出處以及作者網名,未經作者允許不得用于商業目的
C#中數組和集合初始化以及賦值的常見代碼如下:
??????????? //========= 數組 ===========
??????????? //1
??????????? int[] a1 = { 1, 2, 3, 4, 5 };
??????????? //2
??????????? int[] a2 = new int[5] { 1, 2, 3, 4, 5 };
??????????? //3 二維數組
??????????? int[,] a3 = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
??????????? //4 二維數組
??????????? int[,] a4 = new int[3, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
??????????? //5 二維數組
??????????? var a5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
??????????? //6 數組的數組,每個子數組可以有不同的長度
??????????? var a6 = new int[][] { new int[] { 1, 2 }, new int[] { 3, 4 }, new int[] { 5, 6 } };
??????????? for (int i = 0 ;i< a6.Length ;i++)
??????????? {
??????????????? for(int j = 0 ;j< a6[i].Length ;j++)
??????????????????? Console.Write(a6[i][j] + " ");
??????????????? Console.WriteLine("");
??????????? }
??????????? //========= ArrayList ===========
??????????? //1
??????????? ArrayList alst1 = new ArrayList();
??????????? alst1.Add("12");
??????????? alst1.Add(34);
??????????? alst1.Add("56");
??????????? //2
??????????? string[] alstsample = { "12", "34", "56" };
??????????? ArrayList alst2 = new ArrayList(alstsample);
??????????? //3
??????????? object[] alstsample1 = { 12, "ab", 34 };
??????????? ArrayList alst3 = new ArrayList(alstsample1);
??????????? //4
??????????? ArrayList alst4 = new ArrayList();
??????????? alst4.AddRange(new string[] { "12", "34", "56" });
??????????? //5
??????????? ArrayList alst5 = new ArrayList(new string[] { "12", "34", "56" });
??????????? //6
??????????? ArrayList alst6 = new ArrayList(new object[] { "ab", 12, "34" });
??????????? //7
??????????? ArrayList alst7 =new ArrayList(){12, "ab", 34};
??????????? //=========== List =================
??? ????????//1
??????????? List<string> lst1 = new List<string>();
??????????? lst1.Add("12");
??????????? lst1.Add("34");
??????????? lst1.Add("56");
??????????? //2
??????????? string[] lstsample = { "12", "34", "56" };
??????????? List<string> lst2 = new List<string>(lstsample);
??????????? //3
??????????? List<string> lst3 = new List<string>(new string[] { "12", "34", "56" });
??????????? //4
??????????? List<string> lst4 = new List<string>();
??????????? lst4.AddRange(new string[] { "12", "34", "56" });
??????????? //5
??????? List<string> lst5 = new List<string> { "12", "34", "56" };?
學習更多vb.net知識,請參看vb.net 教程 目錄
學習更多C#知識,請參看C#教程 目錄