2주차 복습
개체를 그룹화하고 관리할 때 사용하는 것
1. 배열
2. 컬렉션
List<T>
-컬렉션에서 순회할 떄 foreach외에 for문도 쓸 수 있는것은 List<T>뿐이다. 리스트가 배열이기 때문이다.
+참고) IEnumerable을 쓰고있다 - >foreach를 쓸수있다.
-foreach는 읽기전용
-컬렉션을 생성하기 전에 반드시 인스턴스화해라.
-형식매개변수 : 타입결정
List클래스는 ArrayList에 대응되는 제네릭클래스이다.
-중복을 허용, null값 가능.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
public class App
{
//생성자
public App()
{
//List<T> : 동적 배열
//인스턴스 생성
//아이템 이름들을 관리하는 컬렉션
List<string> itemNames = new List<string>();
//형식매개변수 -> 타입결정
//추가
itemNames.Add("장검");
itemNames.Add("단검");
itemNames.Add("null");
//요소에 접근
Console.WriteLine(itemNames[0]); //장검
//제거
itemNames.Remove("단검"); //맨처음 발견되는 특정개체 제거
//요소의 수 출력
Console.WriteLine(itemNames.Count);
//순회
for(int i=0; i<itemNames.Count; i++)
{//인덱스로 요소에 접근
string itemName = itemNames[i];
Console.WriteLine(itemName);
}
//foreach로 순회. foreach는 읽기전용
foreach(string itemName in itemNames)
{
Console.WriteLine(itemName);
}
}
}
}

Dictionary
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
public class App
{
//생성자
public App()
{
//Dictionary<TKey,TValue)
//키로 요소에 빠르게 접근 가능
// ex) dic[100] : 100 인덱스가 아니다 키다!!
//없는 키로 요소에 접근할 수 없다.
//동일 키 사용 불가(Add할떄)
//인스턴스화
Dictionary<int, string> dicItemNames =
new Dictionary<int, string>();
//추가
dicItemNames.Add(100, "장검");
dicItemNames[101] = "단검";
// dicItemNames.Add(101, "활"); : 동일한 키 사용불가
//검색 (요소에 접근 키로)
Console.WriteLine(dicItemNames[101]);//단검
//삭제 키로 삭제
dicItemNames.Remove(100);
//요소의 수 출력
Console.WriteLine(dicItemNames.Count);
//순회
foreach (KeyValuePair<int, string> pair in dicItemNames )
{
Console.WriteLine("{0},{1}",pair.Key,pair.Value);
}
}
}
}

'C# 기초 복습' 카테고리의 다른 글
Quaternion.identity, LookRotation, 비트 연산자 (0) | 2023.08.21 |
---|---|
Unity- Coroutines(코루틴) (0) | 2023.08.08 |
JSON예제, 대리자 복습 (0) | 2023.07.27 |
배열 예제 복습 (0) | 2023.07.25 |
1주차 복습 (0) | 2023.07.24 |