물론 변경 가능한 구조체 컬렉션을 관리하려면 배열이 필요합니다 .
struct EvilMutableStruct { public double X; } // don't do this
EvilMutableStruct[] myArray = new EvilMutableStruct[1];
myArray[0] = new EvilMutableStruct()
myArray[0].X = 1; // works, this modifies the original struct
List<EvilMutableStruct> myList = new List<EvilMutableStruct>();
myList.Add(new EvilMutableStruct());
myList[0].X = 1; // does not work, the List will return a *copy* of the struct
(가변 구조체의 배열이 바람직한 경우가있을 수 있지만, 일반적으로 배열 내에서 가변 구조체의 다른 동작은 다른 컬렉션과 달리 피해야 할 오류의 원인입니다)
더 심각하게 참조로 요소 를 전달하려면 배열이 필요합니다 . 즉
Interlocked.Increment(ref myArray[i]); // works
Interlocked.Increment(ref myList[i]); // does not work, you can't pass a property by reference
잠금없는 스레드 안전 코드에 유용 할 수 있습니다.
고정 크기 컬렉션 을 기본값으로 빠르고 효율적으로 초기화 하려면 배열이 필요합니다 .
double[] myArray = new double[1000]; // contains 1000 '0' values
// without further initialisation
List<double> myList = new List<double>(1000) // internally contains 1000 '0' values,
// since List uses an array as backing storage,
// but you cannot access those
for (int i =0; i<1000; i++) myList.Add(0); // slow and inelegant
(같은 작업을 수행하는 List의 생성자를 구현할 수 있습니다. c #은이 기능을 제공하지 않습니다.)
컬렉션의 일부 를 효율적으로 복사 하려면 배열이 필요합니다
Array.Copy(array1, index1, array2, index2, length) // can't get any faster than this
double[,] array2d = new double[10,100];
double[] arraySerialized = new double[10*100];
Array.Copy(array2d, 0, arraySerialized, 0, arraySerialized.Length);
// even works for different dimensions
(다시 말해서 이것은 List에도 구현할 수 있지만 C #에는 존재하지 않습니다.)
List<T>