C#數組

來源:互聯網
上載者:User

標籤:text   task   stat   ++   typeof   set   string   turn   ram   

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5 using System.Threading.Tasks;  6   7 namespace ArrayDemo  8 {  9     class Program 10     { 11  12         static void Main(string[] args) 13         { 14             //1 15             int[] myArray; 16             myArray = new int[4]; 17  18             //2 19             int[] myArray2 = new int[4]; 20  21             //3 22             int[] myArray3 = new int[4] { 1, 2, 3, 4 }; 23  24             //4 25             int[] myArray4 = new int[] { 1, 2, 3, 4 }; 26  27             //5 28             int[] myArray5 = { 1, 2, 3, 4 }; 29  30  31             //6 32             var myArray6 = new int[] { 1, 2, 3, 4 }; 33  34             int[] myArray7; 35             myArray7 = new int[] { 1, 3, 4 }; 36             int n = myArray7.Rank;//維度 37  38  39             Array intArray = Array.CreateInstance(typeof(int), 5); 40             for (int i = 0; i < intArray.Length; i++) 41             { 42                 intArray.SetValue(33, i); 43             } 44  45             for (int i = 0; i < 5; i++) 46             { 47                 Console.WriteLine(intArray.GetValue(i)); 48             } 49  50             //CreateInstance()方法有許多重載版本,可以建立多維陣列和不基於0的數組 51             //下面的例子就建立一個包含2X3個元素的二維數組,第一維基於1,第二維基於10 52             int[] lengths = { 2, 3 }; 53             int[] lowerBounds = { 1, 10 }; 54             Array racers = Array.CreateInstance(typeof(Person), lengths, lowerBounds); 55             //SetValue()方法設定數組的元素,其參數是每一維的索引 56             racers.SetValue(new Person 57             { 58                 FirstName = "Alain", 59                 LastName = "Prost" 60             }, index1: 1, index2: 10); 61             racers.SetValue(new Person 62             { 63                 FirstName = "Emerson", 64                 LastName = "Fittipaldi" 65             }, 1, 11); 66             racers.SetValue(new Person 67             { 68                 FirstName = "Emerson", 69                 LastName = "Fittipaldi" 70             }, 1, 12); 71             racers.SetValue(new Person 72             { 73                 FirstName = "Emerson", 74                 LastName = "Fittipaldi" 75             }, 2, 10); 76             racers.SetValue(new Person 77             { 78                 FirstName = "Emerson", 79                 LastName = "Fittipaldi" 80             }, 2, 11); 81             racers.SetValue(new Person 82             { 83                 FirstName = "Emerson", 84                 LastName = "Fittipaldi" 85             }, 2, 12); 86  87  88             Console.ReadKey(); 89         } 90     } 91  92     // 93     public class Person 94     { 95         public string FirstName { get; set; } 96         public string LastName { get; set; } 97         public override string ToString() 98         { 99             return String.Format("{0} {1}", FirstName, LastName);100         }101     }102 }

 

C#數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.