Unity/C#

c# 해쉬테이블

모카쨩 2021. 9. 13. 11:32

 

 

 

쓰기

var hashtable = new Hashtable();
hashtable["intValue"]=1;
hashtable["stringValue"]="2";

 

 

 

읽기

var intValue=(int)hashtable["intValue"];
var stringValue=(string)hashtable["stringValue"];

 

 

매우 쉽다

 

 

 

하지만 해쉬테이블을 쓴다는건 넘겨받는 값이 모호하기 때문일것이다.

따라서 아래같은 상황도 자주 쓰이게 된다

 

 

null체크 후 없으면 생성후 읽기

int intValue;
if(hashtable[nameof(intValue)] == null)
{
    hashtable[nameof(intValue)]=1;
}
intValue=(int)hashtable[nameof(intValue)];


string stringValue;
if(hashtable[nameof(stringValue)] == null)
{
    hashtable[nameof(stringValue)]="2";
}
stringValue=(string)hashtable[nameof(stringValue)];

 

 

간소화버전

int intValue;
intValue=hashtable[nameof(intValue)]??1;

string stringValue;
stringValue=hashtable[nameof(stringValue)]??"2";

 

 

리플과 섞어쓰면 좋다