본문 바로가기
개발언어/C#

[C#] List 사용법 정리

by 창용이랑 2022. 8. 24.
728x90

생성자

var list = new List<int>();

list : (empty)

var listA = new List<int>() {3, 2, 1};

listA : 3, 2, 1

var list = new List<int>(listA);

listB : 3, 2, 1

var list = new List<int>(10);

list.Count : 0

list.Capacity : 10

List[index]

list : 3, 2, 1

int item = list[1];

item : 3

list : 3, 2, 1

list[1] = 4;

list : 3, 4, 1

List.Add

list : 3, 2, 1

list.Add(4);

list : 3, 2, 1, 4

List.AddRange

listA : 1, 2, 3

listB : 4, 5

listA.AddRange(listB);

listA : 1, 2, 3, 4, 5

List.Clear

list : 1, 2, 3

list.Clear();

list : (empty)

List.Contains

list : 1, 2, 3

list.Contains(1) => true

list.Contains(4) => false

List.ConvertAll

listA : 1, 2, 3

var conv = new Converter<int, decimal>(x => (decimal)(x+1));

var listB = listA.ConvertAll<decimal>(conv);

listB : 2.0, 3.0, 4.0

List.CopyTo

list : 1, 2, 3

array : 0, 0, 0, 0, 0

list.CopyTo(array);

array : 1, 2, 3, 0, 0

list : 1, 2, 3

array : 0, 0, 0, 0, 0

list.CopyTo(array, arrayIndex: 2);

array : 0, 0, 1, 2, 3

list : 1, 2, 3

array : 0, 0, 0, 0, 0

list.CopyTo(index: 1, array: array, arrayIndex: 3, count: 1);

array : 0, 0, 0, 2, 0

List.Exists

list : 1, 2, 3

list.Exists(x=> x==3); => true

list.Exists(x=> x>10); => false

List.Equals (동일한 객체인지를 판단)

var listA = new List<int>() {1,2,3};

var listB = listA;

listA.Equals(listB); => true

var listA = new List<int>() {1,2,3};

var listB = new List<int>() {1,2,3};

listA.Equals(listB); => false

List.Find

list : 1, 2, 3

int item = list.Find(x => x>2);

item : 3

list : 1, 2, 3

int item = list.Find(x => x>10);

item : 0

List.FindAll

listA : 1, 2, 3

var listB = listA.FindAll(x => x>1);

listB : 2, 3

listA : 1, 2, 3

var listB = listA.FindAll(x => x>10);

listB : (empty)

List.ForEach

list : 1, 2, 3

list.ForEach(x => { Console.Write(x); });

output : 832

List.Insert

list : 1, 2, 3

list.Insert(index: 1, item: 5);

list : 1, 5, 2, 3

List.InsertRange

listA : 1, 2, 3

listB : 4, 5

listA.InsertRange(index: 1, collection: listB);

listA : 1, 4, 5, 2, 3

List.Remove

list : 1, 4, 2, 4

list.Remove(item: 4);

list: 1, 2, 4

List.RemoveAll

list : 1, 2, 3, 4

list.RemoveAll(x => x<3);

list : 3, 4

List.RemoveAt

list : 1, 2, 3, 4

list.RemoveAt(index: 2);

list : 1, 2, 4

List.RemoveRange

list : 1, 2, 3, 4, 5, 6

list.RemoveRange(index: 2, count:3)

list : 1, 2, 6

List.Reverse

list : 1, 2, 3

list.Reverse();

list : 2, 3, 1

list : 1, 2, 3

list.Reverse(index: 1, count: 2);

list : 1, 3, 2

List.Sort

list : 1, 3, 2, 4

list.Sort(); => list : 1, 2, 3, 4

list.Sort((x,y) => x.CompareTo(y)); => list : 1, 2, 3, 4

list.Sort((x,y) => y.CompareTo(x)); => list : 4, 3, 2, 1

list.Sort(new MyComparer()); => list : 1, 2, 3, 4

public class MyComparer : IComparer<int>

{

public int Compare(int x, int y) { return x.CompareTo(y); }

}

List.ToArray

list : 1, 2, 3

int[] array = list.ToArray();

array : 1, 2, 3

list : (empty)

int[] array = list.ToArray();

array : (empty)

List.TrimExcess

list : 1,2,3,4,5,

list.Count : 5

list.Capacity : 8

list.TrimExcess();

list.Count : 5

list.Capacity : 5