프로그래밍 언어/C#
C# ArrayList와 List 사용방법 및 차이점
소블리애
2019. 12. 4. 16:24
ArrayList는 배열과 유사한 컬렉션입니다.
ArrayList arrayList = new ArrayList();
arrayList.Add(10);
arrayList.Add(20);
arrayList.Add(30);
arrayList.Add("hi");
// 출력결과: 10, 20, 30, hi
arrayList.Insert(1, 25);
// 출력결과: 10, 25, 20, 30, hi
arrayList.Remove("hi");
// 출력결과: 10, 25, 20, 30
arrayList.RemoveAt(0);
// 출력결과: 25, 20, 30
arrayList.Clear();
// 출력결과: 모든 원소가 지워지게 됩니다.
List 사용방법
List<int> list = new List<int>();
list.Add(1);
차이점
ArrayList는 특정 자료형 뿐만 아니라 여러 자료형을 담을 수 있다. 하지만 List는 선언한 자료형에 맞춰서 데이타를 삽입해야 한다. ArrayList가 쓰기 편할만큼 데이타를 가져오거나 넣을때 그만큼 느리다.