게임개발 캠프 15일차

IIRU·4일 전

제네릭 < T >

T 위치에 알맞은 자료형을 넣어준다고 생각하면 됌.

 static void Print<T>(T value)
 {
     Console.WriteLine(value);
 }

이런 함수가 있을 때,

static void Main()
{
    Print(10);
    Print("Hello");
}

10과 hello를 출력하는 것을 볼 수 있다.

T위치에 원하는 자료형이나 클래스를 주고싶다면,

class Inventory<T> where T : Item
{
    public T Value { get; set; }
    public void F(T value) { Console.WriteLine(value.name); }
}

where절을 사용하면 된다.

ArrayList

ArrayList arrayList = new ArrayList();

위와 같이 생성할 수 있다.

arrayList.Add(100); // 100 추가
arrayList.Insert(1, "World"); // 인덱스 1에 "World" 삽입
arrayList.Remove("Hello"); // "Hello" 제거

사용법은 위와같이 사용하면 된다.
!! ArrayList는 반환형이 Object이기 때문에 박싱과 언박싱이 일어날 수 있어 사용을 지향하는 편이다.

List

배열의 한 형식이다.

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

위와 같이 생성할 수 있다.

list.Add(30); // 30 추가
list.Remove(10); // 10 제거
list.Count //list의 갯수

사용법은 위와같이 사용하면 된다.

Stack

스택은 Last in First out(LIFO) 맨 마지막에 넣은게 제일 처음 나오는 자료구조 형태이다.

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

위와 같이 생성할 수 있다.

stack.Push(10);
stack.Pop();
stack.Peek();

사용법은 위와같이 사용하면 된다.

Queue

큐는 First in First out(FIFO) 처음넣은게 제일 처음 나오는 자료구조 형태이다.

Queue<int> queue = new Queue<int>();

위와 같이 생성할 수 있다.

queue.Enqueue(10);
queue.Dequeue();
queue.Peek();

사용법은 위와같이 사용하면 된다.

Dictionary

key 값으로 지정해둔 value를 묶어서 저장하는 자료구조 형태이다.

Dictionary<int, string> dic = new Dictionary<int, string>();

위와 같이 생성할 수 있다.
int 값은 key / string은 value 이다.

dic.Add(1, "first"); //추가
dic.Remove(1); //삭제
dic[1] = "fst";

사용법은 위와같이 사용하면 된다.

profile
초보 개발자 블로그입니다!

0개의 댓글