Get it on Google Play


Wm뮤 :: c# 해쉬테이블

블로그 이미지
가끔 그림그리거나 3D모델링하거나
취미로 로봇만드는
퇴직한 전자과 게임프로그래머
2020.3.48f1 , 2022.3.6f1 주로 사용
모카쨩
@Ahzkwid

Recent Comment

Archive


2021. 9. 13. 11:32 Unity/C#

 

 

 

쓰기

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";

 

 

리플과 섞어쓰면 좋다

 

'Unity > C#' 카테고리의 다른 글

C# Delegate와 IEnumerator  (0) 2021.10.11
자주쓰는 c# 소켓 통신 모음  (0) 2021.09.08
자주쓰는 변수명 모음  (0) 2021.08.18
posted by 모카쨩

  • total
  • today
  • yesterday

Recent Post

저사양 유저용 블로그 진입