C# 심화 문법3

jiyul·2023년 11월 14일
0

C#

목록 보기
8/21

컬렉션

  • 배열과 같이 데이터 집합을 다루는 클래스
네임스페이스이름
SystemArray
System.CollectionsStack, Queue, ArrayList
System.Collections.GenericList<T>, LinkedList<T>, Stack<T>, Queue<T>

어레이

  • 배열을 제어하는 메소드 모음
Array.Sort(arr); //정렬
Array.Reverse(arr); //배열의 요소를 역순으로 변환
Array.ConvertAll(arr, int.Parse); //변환 함수 일괄적용

스택

스택은 탄창이라고 생각하면 된다.

Stack stack = new Stack();
stack.Push();
stack.Peek();
stack.Pop();

큐는 터널이라고 생각하면 된다.

Queue queue = new Queue();
queue.Enqueue();
queue.Dequeue();

stack.Count나 queue.Count는, Pop 이나 Enqueue 등을 통해 값이 변할 수 있으므로
int stack_size = stack.Count; <- 이런 식으로 변수에 저장해 사용하는 것이 좋다.

스택이나 큐를 사용할 때 try 문 내에서 스택이나 큐가 비어있게 되면, catch 문에서 예외 처리 메시지로 '스택(큐)이 비어있습니다.'라는 메시지를 출력할 수 있습니다.

해시테이블

제네릭 컬렉션의 딕셔너리랑 비슷하지만, 보통 해시테이블보다 딕셔너리를 많이 사용한다.

Hashtable hashtable = new Hashtable();

제네릭 컬렉션

  • 컬렉션의 업그레이드 버전
  • 컬렉션과 달리 자료형을 지정해줘야 한다.

스택

Stack<int> i_stack = new Stack<int>();

리스트

List<double> d_list = new List<double>();
d_list.Add(30.409f);

열거형

int size = 10;
var numbers = Enumerable.Range(1, size); //(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
var same_numbers = Enumerable.Repeat(0, size); //(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

딕셔너리

IDictionary<string, string> dict = new Dictionary<string, string>();
dict.Add(key, value);
dict.Remove(key);
dict[key] = value;
dict.ContainsKey(key) : true, false 리턴
dict.TryGetValue(key, out string value) : value 변수에 값 가져오기
foreach(KeyValuePair<string, string> item in dict) : Pair로 가져오기
profile
Let's take the lead

0개의 댓글

관련 채용 정보