--- id: csharp-foreach-loop title: "C# Foreach Loop" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["foreach in C#", "C# foreach 반볡문"] 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", "loops", "foreach"] raw_sources: ["https://www.w3schools.com/cs/cs_foreach_loop.php"] applied_in: [] github_commit: "" --- # [[CSharp Foreach Loop]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) C# spells its range-based loop as the dedicated keyword `foreach (type variableName in arrayName)`, whereas C++11's equivalent range-based for loop reuses the SAME `for` keyword with a colon (`for (type var : container)`) β€” same underlying concept (iterate every element without manual indexing) expressed through two genuinely different keywords, meaning "foreach" isn't just C++'s range-for renamed, it's C#'s own dedicated syntax that happens to solve the identical problem. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`foreach (type variableName in arrayName) { ... }`** β€” a loop construct used EXCLUSIVELY for iterating over elements of an array (or other data sets); no index variable, no manual bounds. [S1] - **`in` keyword** β€” connects the per-element variable to the collection being iterated, distinct from C++'s `:` colon syntax for the same relationship. [S1] - **No indexing needed** β€” unlike a `for` loop over an array (`arr[i]`), `foreach` never exposes an index; you only get direct access to each element's value. [S1] - **Deferred prerequisite** β€” the tutorial explicitly tells readers not to worry about the array syntax shown here, deferring full array coverage to the later Arrays chapter. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Example: `string[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; foreach (string i in cars) { Console.WriteLine(i); }` β€” prints all four car names, one per line. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **forκ°€ μ•„λ‹ˆλΌ 별도 ν‚€μ›Œλ“œ foreachλ₯Ό μ‚¬μš©ν•¨**: C++11의 λ²”μœ„ 기반 for 루프(`for (string car : cars)`)λŠ” κΈ°μ‘΄ `for` ν‚€μ›Œλ“œλ₯Ό 콜둠(:)κ³Ό ν•¨κ»˜ μž¬μ‚¬μš©ν–ˆμ§€λ§Œ, C#은 `foreach`λΌλŠ” μ™„μ „νžˆ λ³„λ„μ˜ μ „μš© ν‚€μ›Œλ“œμ™€ `in`을 μ‚¬μš©ν•œλ‹€λŠ” 점이 확인됨 β€” κ°œλ…μ€ 동일(인덱슀 없이 각 μ›μ†Œ 순회)ν•˜μ§€λ§Œ 문법 ν‘œν˜„μ΄ μ‹€μ œλ‘œ 닀름. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) μžλ™μ°¨ 이름이 λ‹΄κΈ΄ λ¬Έμžμ—΄ 배열을 foreach둜 μˆœνšŒν•΄ 각 μ›μ†Œλ₯Ό 좜λ ₯ν•˜λŠ” μ˜ˆμ œκ°€ μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) foreach β€” dedicated keyword + `in`, unlike C++'s `for ... :` syntax (C#): ```csharp string[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; 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 For Loop]], [[CSharp Break]], [[CSharp Arrays]], [[CPP For Loop Foreach]] - **μ°Έμ‘° λ§₯락:** Loops μ„Ήμ…˜ β€” Break and Continue μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C# Foreach Loop β€” https://www.w3schools.com/cs/cs_foreach_loop.php ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C# Foreach Loop" page (Astra wiki-curation, P-Reinforce v3.1 format).