C#으로 툴작업하면서 자주 사용하게 된 List와 Dictionary의 사용 예들.
// List에 값 삽입
List<string> list = new List<string>();
list .Add("값1");
list .Add("값2");
list .Add("값3");
// Dictionary에 값 삽입
Dictionary<String, List<String>> dic = new Dictionary<String, List<String>>();
dic.Add("키1", "값1");
dic.Add("키2", "값2");
dic.Add("키3", "값3");
// List 원하는 값 찾기
String strResult = list .Find(
delegate(String strTmp)
{
// 원하는 값을 판별
return strTmp == "값1";
}
);
if (null == strResult)
// 찾는 값이 없을 경우
else
// 찾는 값을 찾은 경우
// Dic 키를 이용한 값 반환
String strValue;
if (dic.TryGetValue("키1", out strValue))
{
// strValue 사용
}
// List 순차 검색
foreach (String strValue in list )
{
// str 사용
}
// Dic 순차 검색
{
foreach (KeyValuePair<String, String> dicvalue in dic)
{
// dicvalue.Key 사용
// dicvalue.Value 사용
}
}