1.區別:ios
2.簡單數組:數組
//自定義Person類 public class Person { public string FirstName {get; set;} public string LastName {get; set;} public override string ToString() => $"{FirstName} {LastName}"; } //定義Person數組 Person[] myPersons = new Person[2]; //爲每一個數組元素分配內存 myPersons[0] = new Person {FirstName = "Ayrton", LastName="Senna"}; myPersons[1] = new Person {FirstName = "Michael", LastName="Schumacher"};
Person[] myPersons2 = { new Person {FirstName = "Ayrton", LastName="Senna"}, new Person {FirstName = "Michael", LastName="Schumacher"} };
3.多維數組:用兩個或多個整數來索引ide
int[,] towdim = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, };
int[,,] threedim = { { {1, 2}, {3, 4} }, { {5, 6}, {7, 8} }, { {9, 10}, {11, 12} } }; WriteLine(threedim[0, 1, 1]);
4. 鋸齒數組:每一行均可以有不一樣的大小。spa
int[][] jagged = new int[3][]; jagged[0] = new int[2] {1, 2}; jagged[1] = new int[6] {3, 4, 5, 6, 7, 8}; jagged[2] = new int[3] {9, 10, 11};
5. Array類:用方括號聲明數組是C#中使用Array類的表示法.net
//建立類型爲int、大小爲5的一維數組 Array intArray1 = Array.CreateInstatnce(typeof(int), 5); //強制轉換成聲明爲int[]的數組 int[] intArray2 = (int[])intArray1; //建立類型爲Person,大小爲2*3個元素的二維數組 //且第一維基於1,第二維基於10 int[] lengths = {2, 3}; int[] lowerBounds = {1, 10}; Array racers = Array.CreateInstance(typeof(Person), lengths, lowerBounds);
註明:詳細可參考 原文:https://blog.csdn.net/ios99999/article/details/77800819code
//自定義Person類 public class Person : IComparable<Person> { public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() => $"{FirstName} {LastName}"; public int CompareTo(Person other) { if (other == null) throw new ArgumentNullException("other"); int result = LastName.CompareTo(other.LastName); if (result == 0) { result = FirstName.CompareTo(other.FirstName); } return result; } } //數組排序 Person[] persons = { new Person { FirstName="Damon", LastName="Hill" }, new Person { FirstName="Niki", LastName="Lauda" }, new Person { FirstName="Ayrton", LastName="Senna" }, new Person { FirstName="Graham", LastName="Hill" } }; Array.Sort(persons); foreach (Person p in persons) { WriteLine(p); } //排序結果 //Damon Hill //Graham Hill //Niki Lauda //Ayrton Senna
//自定義PersonComparer類 //定義了排序選項 public enum PersonCompareType { FirstName, LastName } public class PersonComparer : IComparer<Person> { private PersonCompareType _compareType; public PersonComparer(PersonCompareType compareType) { _compareType = compareType; //肯定排序方式 } #region IComparer<Person> Members // 可根據排序方式靈活進行排序 public int Compare(Person x, Person y) { if (x == null && y == null) return 0; if (x == null) return 1; if (y == null) return -1; switch (_compareType) { case PersonCompareType.FirstName: return string.Compare(x.FirstName, y.FirstName); case PersonCompareType.LastName: return string.Compare(x.LastName, y.LastName); default: throw new ArgumentException( "unexpected compare type"); } } #endregion } //自定義類型數組排序,根據FirstName排序 Array.Sort(persons, new PersonComparer(PersonCompareType.FirstName)); foreach (Person p in persons) { WriteLine(p); } //排序結果 //Ayrton Senna //Damon Hill //Graham Hill //Niki Lauda
6. 數組做爲參數:數組能夠做爲參數傳遞給方法,也能夠從方法返回對象
//計算數組段定義的全部整數之和 static int SumOfSegments(ArraySegment<int>[] segments) { int sum = 0; foreach (var segment in segments) { for (int i = segment.Offset; i < segment.Offset + segment.Count; i++) { sum += segment.Array[i]; } } return sum; } //使用方法 //計算數組ar1從第1個元素開始,引用3個元素; //計算數組ar2從第3個元素開始,引用3個元素; int[] ar1 = { 1, 4, 5, 11, 13, 18 }; int[] ar2 = { 3, 4, 5, 18, 21, 27, 33 }; var segments = new ArraySegment<int>[2] { new ArraySegment<int>(ar1, 0, 3), new ArraySegment<int>(ar2, 3, 3) }; // sum = 1 + 4 + 5 + 18 + 21 + 27 var sum = SumOfSegments(segments); WriteLine($"sum of all segments: {sum}");
7. 枚舉:使用foreach語句實現迭代,是因爲其使用了枚舉器blog
註明: 詳細可參考 原文:https://blog.csdn.net/i1tws/article/details/51511134?utm_source=copy排序
8.元組:用於合併不一樣類型的對象。索引
9.結構比較:數組和元組都實現接口IStructuralEquatable和IStructuralComparable