List<int> intList = new List<int> intList;
string[] name = new string[3];
var greeting = "Hello";
Console.WriteLine(greeting.GetType()); // output: System.String
var a = 32;
Console.WriteLine(a.GetType()); // output: System.Int32
var a = 32L;
Console.WriteLine(a.GetType()); // output: System.Int64 // 정수L로 long형
var xs = new List<double>();
Console.WriteLine(xs.GetType()); // output: System.Collections.Generic.List`1[System.Double]
using System.Collections.Generic;으로 선언해주어 사용[It] represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.
List<T> items = new List<T>(); // 리스트 선언
List<int> numbers = new List<int>() { 1, 2, 3 }; // 선언 및 초기화
List<string> languages = new List<string>(); // 문자열 자료형 list 선언
languages.Add("C#"); // list는 add기능 이용가능
languages.Add("Kotlin");
languages.Add(10); // 문자열이 아닌 다른 자료형이므로 compilation error!
ArrayList를 이용하면 자료형에 구애받지 않고 자유롭게 사용 가능ArrayList items = new ArrayList();
items.Add(10);
items.Add(true);
items.Add("this wasn't supposed to be here!");
var n = new List<int>();
n.Add(15);
n.Add(30);
n.Add(42);
n이라는 정수형 list를 먼저 선언해주고, 3개의 정수값을 넣어주는 기본적인 방법
n.Insert(index, item);삽입할 위치 값인 index와 넣을 값인 item을 통해 넣어줄 수 있다.
for (int i = 0; i < n.Count; i++)
{
Debug.Log($"{i + 1}번째 : {n[i]}");
}
Count를 통해 list의 길이를 구할 수 있다.
foreach(var x in n)
{
Debug.Log(x);
}
myList.Sort() : 오름차순으로 정렬
myList.Reverse() : 내림차순으로 정렬
n.Contains(item)
list에 item값이 있는지 확인하는 함수. True or False를 반환
n.Indexof(item)
list의 item이 어느 위치에 있는지 반환. 없다면 -1
Remove
n.Remove(item)
첫 번째 item 값을 지움
RemoveAll
n.RemoveAll(Predicate<T> match)
리스트에서 특정 조건을 만족시키는 모든 값들을 제거
RemoveAt
n.RemoveAt(index)
리스트의 특정 위치에 있는 값 제거
RemoveRange
n.RemoveRange(index, count)
리스트에서 index 위치로부터 count개수의 값 제거
Clear
n.Clear()
리스트의 모든 요소를 제거
List<char> alphabet = new List<char>();
...
alphabet = null; // List를 더 이상 참조하지 않음