C#교과서 마스터하기 17. 컬렉션(Collection)

min seung moon·2021년 7월 10일
0

C#

목록 보기
17/54

https://www.youtube.com/watch?v=-gDdb28VcwQ&list=PLO56HZSjrPTB4NxAsEP8HRk6YKBDLbp7m&index=47

1. 컬렉션(Collection)

  • 배열, 리스트, 딕셔너리(사전)을 사용하여 관련 개체의 그룹을 만들고 관리
  • 컬렉션 키워드 : array, list, dictionary
  • 컬렉션 클래스(제네릭) : Array, Stack, Queue, ArrayList, HashTable

01. Variable(변수)

> int number = 1_234;
> number
1234

02. Array(배열)

> string[] colors = {"red", "green", "blue"};
> colors[0]
"red"
> colors[1]
"green"
> colors[2]
"blue"
> colors[3]
System.IndexOutOfRangeException
> Array.Sort(colors) // abc..., 가나다... 순으로 정렬
> foreach(string color in colors)
. {
.	Console.WriteLine(color);
. }
blue
green
red
> Array.Reverse(colors) // 역순
> foreach(string color in colors)
. {
.	Console.WriteLine(color);
. }
red
green
blue

03. List(리스트)

  • 배열(array)과 리스트(list)의 차이
    • 배열이 리스트 보다 빠르다
    • 리스트가 배열보다 사용하기 편하다
    • 배열은 고정 길이인 반면 리스트는 가변 길이이다
> using System.Collections;
> ArrayList list = new ArrayList();
> list.Add(100);
> list.Add(100);
> list.RemoveAt(1);
> list.Add(200);
> list[0]
100
> list[1]
200
> list.Insert(0, 50);
> list
ArrayList(3) { 50, 100, 200 }

04. Hashtable(딕셔너리)

> using System.Collections;
> Hashtable hashtable = new Hashtable();
> hashtable[0] = "DotNetKorea";
> hashtable["NickName"] = "RedPlus";
> hashtable[0]
"DotNetKorea"
> hashtable["NicName"]
"RedPlus"

profile
아직까지는 코린이!

0개의 댓글