Search

배열 - 각 타입별 배열

배열은 문자열 말고도 타입에 맞는 배열들을 생성할 수 있습니다
문자 배열
string[] game = new game[3]; game[0] = "Leage of Legends" game[1] = "메이플 스토리" Console.WriteLine(game[0]); // 출력 - Leage of Legends Console.WriteLine(game[1]); // 출력 - 메이플 스토리
C#
복사
정수 배열
int[] year = new int[4]; year[0] = 2020; year[1] = 2021; year[2] = 2022; year[3] = 2023; Console.WriteLine(year[0]); // 출력 - 2020 Console.WriteLine(year[1]); // 출력 - 2021 Console.WriteLine(year[2]); // 출력 - 2022 Console.WriteLine(year[3]); // 출력 - 2023
C#
복사
실수 배열
float[] height = new float[4]; height[0] = 164.5f; height[1] = 172.7f; height[2] = 181.2f; Console.WriteLine(height[0]); // 출력 - 164.5 Console.WriteLine(height[1]); // 출력 - 172.7 Console.WriteLine(height[2]); // 출력 - 181.2
C#
복사
배열의 타입이 맞지 않는 다면 에러가 발생합니다
string[] game = new game[3]; game[0] = "Leage of Legends"; // 정상 - string 배열에 string 저장 game[1] = 2020; // 에러 - string 배열에 int 저장 game[2] = 164.5; // 에러 - string 배열에 float 저장
C#
복사