Search

배열에 데이터가 없다면? - 기본값

문자 3개가 저장가능한 game 배열에 2개까지 데이터를 저장하고 출력시 다음과 같이 표시됩니다.
string[] game = new string[3]; game[0] = "League of Legends"; game[1] = "메이플 스토리"; Console.WriteLine(game[0]); // 출력 - League of Legends Console.WriteLine(game[1]); // 출력 - 메이플 스토라
C#
복사
그럼 값을 저장하지 않은 2호실은 어떻게 될까요?
string[] game = new string[3]; game[0] = "League of Legends"; game[1] = "메이플 스토리"; Console.WriteLine(game[0]); // 출력 - League of Legends Console.WriteLine(game[1]); // 출력 - 메이플 스토라 Console.WriteLine(game[2]); // 출력 -
C#
복사
실행해보면 아무것도 나오지 않는 것을 볼수 있습니다. 데이터가 없는게 아니라 “” 이 들어가 있는 상태입니다.
이를 기본값이라고 하며 자료형 항목에서 확인 할 수 있습니다.
문자의 기본형은 ””이기 때문에 아무것도 나타나지 않습니다.
숫자와 불리언의 경우 각각의 기본값이 나타나게 됩니다.
int[] year = new int[2]; Console.WriteLine(year[0]); // 출력 - 0 bool[] check = new bool[2]; Console.WriteLine(check[0]); // 출력 - false
C#
복사