--- id: csharp-arrays-multi title: "C# Multidimensional Arrays" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["2D array C#", "GetLength()", "C# 다차원 배열"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.83 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["csharp", "programming-language", "w3schools", "arrays", "multidimensional"] raw_sources: ["https://www.w3schools.com/cs/cs_arrays_multi.php"] applied_in: [] github_commit: "" --- # [[CSharp Multidimensional Arrays]] ## 🎯 한 줄 통찰 (One-line insight) C#'s true 2D array uses ONE bracket pair with an internal comma (`int[,] numbers`), fundamentally different from C's stacked-bracket 2D array (`int numbers[2][3]`, effectively an array of arrays) — and this single-object design is why C# needs a NEW method, `GetLength(dimension)`, instead of reusing `.Length` (which on a 2D array would only report the total flattened element count, not a per-dimension size), forcing a second, dimension-aware sizing API that C's simpler nested-array model never required. [S1] ## 🧠 핵심 개념 (Core concepts) - **`int[,] name = { {...}, {...} };`** — a single comma inside one bracket pair signals a 2D array; two commas (`[,,]`) would signal 3D. [S1] - **Row/column indexing** — `numbers[row, col]` — a SINGLE index expression with two comma-separated values, not `numbers[row][col]` (double bracket, as in C's array-of-arrays model). [S1] - **Element access & mutation** — `numbers[0, 2]` reads row 0, column 2; `numbers[0, 0] = 5;` writes to row 0, column 0. [S1] - **`foreach` over a 2D array** — flattens automatically: `foreach (int i in numbers)` visits every element across all rows without needing nested loops. [S1] - **`GetLength(dimension)`** — replaces `.Length` for multidimensional sizing; `GetLength(0)` returns the row count, `GetLength(1)` returns the column count — one loop bound per dimension. [S1] - **Nested `for` loop required for row/column-aware iteration** — one loop per dimension, using `GetLength(0)` and `GetLength(1)` as the respective bounds. [S1] ## 📖 세부 내용 (Details) - Declaration: `int[,] numbers = { {1, 4, 2}, {3, 6, 8} };` — two "rows," each with three "columns." [S1] - Access: `Console.WriteLine(numbers[0, 2]); // 2` (row 0, column 2). [S1] - Modify: `numbers[0, 0] = 5; Console.WriteLine(numbers[0, 0]); // 5`. [S1] - Flattened foreach: `foreach (int i in numbers) { Console.WriteLine(i); }` — prints all 6 elements in row-major order. [S1] - Dimension-aware nested for: `for (int i = 0; i < numbers.GetLength(0); i++) { for (int j = 0; j < numbers.GetLength(1); j++) { Console.WriteLine(numbers[i, j]); } }`. [S1] ## ⚖️ 모순 및 업데이트 (Contradictions & updates) - **C의 배열의 배열 모델과 근본적으로 다른 단일 객체 설계**: C의 2D 배열(`int numbers[2][3]`)은 사실상 배열의 배열이라 `numbers[i][j]`처럼 대괄호를 두 번 쓰지만, C#의 `int[,] numbers`는 콤마로 차원을 표시하는 단일 객체이며 `numbers[i, j]`처럼 하나의 인덱스 표현식 안에 콤마로 두 값을 전달한다는 점이 확인됨. [S1] - **Length 대신 GetLength(dimension)이 필요해짐**: 1차원 배열의 `.Length` 프로퍼티는 다차원 배열에서는 차원별 크기를 알려주지 못하므로, C#은 `GetLength(0)`/`GetLength(1)`처럼 차원 인덱스를 받는 별도 메서드를 도입했다는 점이 새로운 차이로 확인됨. [S1] ## 🛠️ 적용 사례 (Applied in summary) 2행 3열의 정수 2D 배열을 선언하고, 특정 원소를 읽고/바꾸고, foreach와 이중 for 루프(GetLength 사용) 두 가지 방식으로 순회하는 예제가 원문에서 직접 실전 활용 사례로 제시됨. [S1] ## 💻 코드 패턴 (Code patterns) 2D array — single bracket + comma indexing, GetLength() for dimension-aware loops (C#): ```csharp int[,] numbers = { {1, 4, 2}, {3, 6, 8} }; Console.WriteLine(numbers[0, 2]); // 2 (row 0, col 2) for (int i = 0; i < numbers.GetLength(0); i++) // rows { for (int j = 0; j < numbers.GetLength(1); j++) // columns { Console.WriteLine(numbers[i, j]); } } ``` ## ✅ 검증 상태 및 신뢰도 - **상태:** draft - **검증 단계:** conceptual - **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body) - **신뢰 점수:** 0.83 - **중복 검사 결과:** 신규 생성 (New discovery) ## 🔗 지식 그래프 (Knowledge Graph) - **상위/루트:** [[C# Tutorial]] - **관련 개념:** [[CSharp Arrays Sort]], [[CSharp Methods]], [[C Arrays]] - **참조 맥락:** Arrays 섹션의 마지막 챕터 — Methods 섹션으로 이어짐. ## 📚 출처 (Sources) - [S1] W3Schools — C# Multidimensional Arrays — https://www.w3schools.com/cs/cs_arrays_multi.php ## 📝 변경 이력 (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C# Multidimensional Arrays" page (Astra wiki-curation, P-Reinforce v3.1 format).