본문 바로가기

C#/기본 문법

C# 다차원 배열, GetLength()

 

 

 

 

 

전체 코드


 

namespace Rookiss_CSharp
{
    class Program()
    {
        class Map
        {
            // 1 : 갈 수 없는 벽(빨간색)
            // 0 : 갈 수 있음(초록색)
            int[,] tiles = {
                { 1, 1, 1, 1, 1},
                { 1, 0, 0, 0, 1},
                { 1, 0, 0, 0, 1},
                { 1, 0, 0, 0, 1},
                { 1, 1, 1, 1, 1}
            };


            public void Render()
            {
                // int[2, 5] tiles
                
                // .Length : 배열의 전체 원소 개수
                // tiles.Length ->  2 * 5 = 10

                // .GetLength(0) : 첫 번째 차원(행)의 길이를 반환
                // tiles.GetLength(0) -> 2
                
                // .GetLength(1) : 두 번째 차원(열)의 길이를 반환
                // tiles.GetLength(1) -> 5



                // int[1, 2, 3] arr
                // arr.GetLength(0) -> 1
                // arr.GetLength(1) -> 2
                // arr.GetLength(2) -> 3

                ConsoleColor defaultColor = Console.ForegroundColor;

                for (int y = 0; y < tiles.GetLength(0); y++)
                {
                    for (int x = 0; x < tiles.GetLength(1); x++)
                    {
                        if (tiles[y, x] == 1)
                            Console.ForegroundColor = ConsoleColor.Red;
                        else
                            Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write("\u25cf");
                    }
                    Console.WriteLine();
                }

                Console.ForegroundColor = defaultColor;
            }
        }


        static void Main(string[] args)
        {
            // 배열
            int[] scores = new int[5] { 10, 30, 40, 20, 50 };

            // 2차원 배열
            // 3가지 초기화 방법이 있음
            // int[,] arr = new int[2, 3] { { 1, 2, 3 }, { 1, 2, 3} };
            // int[,] arr = new int[,] { { 1, 2, 3 }, { 1, 2, 3} };
            int[,] arr = { { 1, 2, 3 }, { 1, 2, 3 } };

            // 2F [ . . .]
            // 1F [ . . .]

            arr[0, 0] = 1;
            arr[1, 0] = 1;

            Map map = new Map();
            map.Render();

            // 가변 배열 : 고정없이 유동적으로 사용
            // 1층 : 호실 3개, 2층 : 호실 5개, 3층 : 호실 1개 ..
            // 각 크기가 달라짐

            // 2번째 부분은 값 고정안하고 사용
            int[][] a = new int[3][];

            // 1층 : 호실 3개, 2층 : 호실 5개, 3층 : 호실 1개 ..
            a[0] = new int[3];
            a[1] = new int[5];
            a[2] = new int[1];

            a[0][0] = 1;
        }
    }
}

 

 

 

 

 

 

 


 

 

 

출처


https://www.inflearn.com/course/%EC%9C%A0%EB%8B%88%ED%8B%B0-mmorpg-%EA%B0%9C%EB%B0%9C-part1?srsltid=AfmBOopkXVxKs-MQPYZkTjEYW-yIDMO5xP7l5hQq0hQasUWhobvyKO6N

 

[C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part1: C# 기초 프로그래밍 입문| Rookiss - 인프런 강

현재 평점 4.9점 수강생 6,987명인 강의를 만나보세요. 기초 프로그래밍 지식이 없는 사람들을 위한 C# 프로그래밍 기초 강의입니다. 문법 암기 위주의 수업이 아니라, 최대한 필요한 부분만을 요

www.inflearn.com

51강 다차원 배열

 

'C# > 기본 문법' 카테고리의 다른 글

C# Dictionary  (0) 2025.11.20
C# List  (0) 2025.11.20
C# 배열 연습문제  (0) 2025.11.18
C# 배열  (0) 2025.11.13
C# TextRPG2  (0) 2025.11.13