--- id: csharp-arrays-loop title: "C# Loop Through Arrays" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["for loop array C#", "foreach array C#", "C# λ°°μ—΄ 순회"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.82 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["csharp", "programming-language", "w3schools", "arrays", "loops"] raw_sources: ["https://www.w3schools.com/cs/cs_arrays_loop.php"] applied_in: [] github_commit: "" --- # [[CSharp Arrays Loop]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) This chapter directly recycles the exact `foreach` example already shown in the Loops section's Foreach chapter β€” same `cars` array, same code β€” but now frames it explicitly AGAINST the indexed `for` loop alternative, making an explicit value judgment the earlier chapter didn't state outright: `foreach` is "easier to write... does not require a counter... more readable," a direct trade-off statement this wiki hasn't seen phrased this bluntly for C or C++'s equivalent range-based loops. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Indexed `for` loop over an array** β€” uses `.Length` as the loop bound: `for (int i = 0; i < cars.Length; i++) { Console.WriteLine(cars[i]); }`. [S1] - **`foreach` loop over an array** β€” no counter, no `.Length`, no index: `foreach (string i in cars) { Console.WriteLine(i); }`. [S1] - **Explicit trade-off stated** β€” `foreach` is easier to write, doesn't need a counter, and is more readable than the indexed `for` β€” a direct recommendation, not just a neutral alternative. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Indexed loop: `string[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; for (int i = 0; i < cars.Length; i++) { Console.WriteLine(cars[i]); }`. [S1] - Foreach loop (identical to the earlier Foreach Loop chapter's own example): `string[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; foreach (string i in cars) { Console.WriteLine(i); }`. [S1] - Reading aid: "for each string element (called i β€” as in index) in cars, print out the value of i." [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **foreachκ°€ for보닀 λͺ…μ‹œμ μœΌλ‘œ ꢌμž₯됨**: 이전 Foreach Loop μ±•ν„°λŠ” foreachλ₯Ό λ‹¨μˆœνžˆ μ†Œκ°œλ§Œ ν–ˆμ§€λ§Œ, 이번 μ±•ν„°λŠ” for와 λ‚˜λž€νžˆ λΉ„κ΅ν•˜λ©° "μΉ΄μš΄ν„°κ°€ ν•„μš” μ—†κ³  더 읽기 쉽닀"κ³  λͺ…μ‹œμ μœΌλ‘œ μš°μœ„λ₯Ό μ„ μ–Έν•œλ‹€λŠ” 점이 확인됨 β€” 두 방식이 λ™λ“±ν•œ λŒ€μ•ˆμ΄ μ•„λ‹ˆλΌ foreachκ°€ ꢌμž₯λ˜λŠ” μ„ νƒμž„μ„ λΆ„λͺ…νžˆ 함. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) λ™μΌν•œ μžλ™μ°¨ 이름 배열을 인덱슀 기반 for 루프와 foreach 루프 두 κ°€μ§€λ‘œ μˆœνšŒν•΄ λΉ„κ΅ν•˜λŠ” μ˜ˆμ œκ°€ μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Indexed for loop vs. foreach β€” same output, different readability (C#): ```csharp string[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; for (int i = 0; i < cars.Length; i++) { Console.WriteLine(cars[i]); } foreach (string i in cars) { Console.WriteLine(i); } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.82 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C# Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CSharp Arrays]], [[CSharp Arrays Sort]], [[CSharp Foreach Loop]] - **μ°Έμ‘° λ§₯락:** Arrays μ„Ήμ…˜ β€” Sort Arrays μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C# Loop Through Arrays β€” https://www.w3schools.com/cs/cs_arrays_loop.php ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C# Loop Through Arrays" page (Astra wiki-curation, P-Reinforce v3.1 format).