--- id: csharp-constructors title: "C# Constructors" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["default constructor C#", "constructor parameters C#", "C# μƒμ„±μž"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.85 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["csharp", "programming-language", "w3schools", "oop", "constructors"] raw_sources: ["https://www.w3schools.com/cs/cs_constructors.php"] applied_in: [] github_commit: "" --- # [[CSharp Constructors]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) The "constructors save time" comparison at the end of this chapter directly rewrites the SAME Ford/Opel two-object example already shown across Class Members and Multiple Classes chapters β€” turning 8 lines of manual field assignment (`Ford.model = "Mustang"; Ford.color = "red"; Ford.year = 1969;` Γ—2) into 2 lines of parameterized construction (`new Car("Mustang", "Red", 1969)`), making constructors the payoff for a pattern this wiki has now seen written out the long way THREE times before finally being shown the short way β€” identical in spirit to C++'s constructor mechanism already covered in Topic_CPP, right down to the "name must match the class, no return type" rule and constructor overloading via differing parameter counts. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Constructor** β€” a special method that initializes an object; called automatically when an object of the class is created (`new Car()`). [S1] - **Naming rule** β€” the constructor's name MUST match the class name exactly, and it CANNOT have a return type (no `void`, no `int`, nothing). [S1] - **Default constructor** β€” every class has one even if you don't write it yourself; but the auto-generated one can't set initial field values the way a custom constructor can. [S1] - **Constructor parameters** β€” a constructor can take parameters just like any method, used to set field values at creation time (`public Car(string modelName) { model = modelName; }`). [S1] - **Multiple parameters** β€” a constructor can take as many parameters as needed, mapping each to a field. [S1] - **Constructor overloading** β€” like ordinary methods, constructors can be overloaded with different parameter counts/types. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Parameterless constructor setting a fixed value: `class Car { public string model; public Car() { model = "Mustang"; } static void Main(string[] args) { Car Ford = new Car(); Console.WriteLine(Ford.model); } }` β†’ `"Mustang"`. [S1] - Single-parameter constructor: `public Car(string modelName) { model = modelName; } ... Car Ford = new Car("Mustang");` β†’ `"Mustang"`. [S1] - Multi-parameter constructor: `public Car(string modelName, string modelColor, int modelYear) { model = modelName; color = modelColor; year = modelYear; } ... Car Ford = new Car("Mustang", "Red", 1969);` β†’ `"Red 1969 Mustang"`. [S1] - Before/after comparison: manual field-by-field assignment for `Ford` and `Opel` (8 lines) collapses to `Car Ford = new Car("Mustang", "Red", 1969); Car Opel = new Car("Astra", "White", 2005);` (2 lines) with a parameterized constructor. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **C++ μƒμ„±μž κ·œμΉ™κ³Ό 동일**: μƒμ„±μž 이름이 클래슀λͺ…κ³Ό μΌμΉ˜ν•΄μ•Ό ν•˜κ³  λ°˜ν™˜ νƒ€μž…μ„ κ°€μ§ˆ 수 μ—†λ‹€λŠ” κ·œμΉ™, 그리고 λ§€κ°œλ³€μˆ˜ 개수둜 μ˜€λ²„λ‘œλ”©ν•  수 μžˆλ‹€λŠ” κ·œμΉ™μ΄ Topic_CPP의 Constructors/Constructors Overloading 챕터와 차이가 μ—†λ‹€λŠ” 점이 확인됨. [S1] - **이전 μ±•ν„°λ“€μ˜ μˆ˜λ™ ν•„λ“œ λŒ€μž… νŒ¨ν„΄μ΄ μƒμ„±μžλ‘œ 압좕됨**: Class Members와 Multiple Classes μ±•ν„°μ—μ„œ 반볡적으둜 보여쀀 "객체 생성 ν›„ ν•„λ“œλ₯Ό ν•˜λ‚˜μ”© λŒ€μž…"ν•˜λŠ” 8μ€„μ§œλ¦¬ νŒ¨ν„΄μ΄, 이번 μ±•ν„°μ˜ λ§€κ°œλ³€μˆ˜ μƒμ„±μžλ₯Ό μ“°λ©΄ 2μ€„λ‘œ μ€„μ–΄λ“ λ‹€λŠ” 것이 직접적인 λΉ„κ΅λ‘œ 확인됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) Ford와 Opel 두 Car 객체λ₯Ό μƒμ„±μž 없이 ν•„λ“œλ³„λ‘œ μˆ˜λ™ λŒ€μž…ν•˜λŠ” 방식과, 3개 λ§€κ°œλ³€μˆ˜λ₯Ό λ°›λŠ” μƒμ„±μžλ‘œ ν•œ 쀄에 μƒμ„±ν•˜λŠ” 방식을 λ‚˜λž€νžˆ λΉ„κ΅ν•˜λŠ” μ˜ˆμ œκ°€ μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Manual field assignment vs. parameterized constructor β€” same result, less code (C#): ```csharp // Without constructor Car Ford = new Car(); Ford.model = "Mustang"; Ford.color = "red"; Ford.year = 1969; // With constructor class Car { public string model, color; public int year; public Car(string modelName, string modelColor, int modelYear) { model = modelName; color = modelColor; year = modelYear; } } Car Ford2 = new Car("Mustang", "Red", 1969); ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.85 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C# Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CSharp Classes Multi]], [[CSharp Access Modifiers]], [[CPP Constructors]], [[CPP Constructors Overloading]] - **μ°Έμ‘° λ§₯락:** Constructors μ„Ήμ…˜μ˜ 유일 챕터 β€” Access Modifiers & Properties μ„Ήμ…˜μœΌλ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C# Constructors β€” https://www.w3schools.com/cs/cs_constructors.php ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C# Constructors" page (Astra wiki-curation, P-Reinforce v3.1 format).