--- id: csharp-enums title: "C# Enum" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["enumerations C#", "enum switch C#", "C# μ—΄κ±°ν˜•"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.84 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["csharp", "programming-language", "w3schools", "enums"] raw_sources: ["https://www.w3schools.com/cs/cs_enums.php"] applied_in: [] github_commit: "" --- # [[CSharp Enums]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) `Console.WriteLine(myVar)` on a C# enum prints the symbolic NAME (`"Medium"`), not the underlying integer β€” a real behavioral difference from C++'s enum, which prints its raw integer value when streamed through `cout <<` unless a custom conversion is written, meaning C# enums carry name-string-printability as a built-in feature (via an implicit `ToString()`) where C++ requires the programmer to bridge that gap manually; the auto-incrementing 0/1/2... numbering and the ability to override a starting value (which cascades to all subsequent members) otherwise matches the C++ enum model already documented in `[[CPP Enum]]`. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`enum` keyword** β€” declares an enum instead of `class`/`interface`; members separated by commas, no explicit values required. [S1] - **Dot syntax access** β€” `Level myVar = Level.Medium;` β€” same pattern as accessing a static class member. [S1] - **Printing an enum prints its NAME** β€” `Console.WriteLine(myVar);` outputs `"Medium"`, not a number. [S1] - **Default integer values** β€” the first member is `0`, the second `1`, and so on, incrementing automatically. [S1] - **Explicit cast to get the integer** β€” `(int) Months.April` is required to retrieve the underlying numeric value; it isn't exposed by default. [S1] - **Custom starting values cascade forward** β€” assigning a value to one member (e.g. `March = 6`) shifts every SUBSEQUENT member's auto-incremented value accordingly (`April` becomes `7`, not `3`). [S1] - **Enums inside a class** β€” an enum can be declared as a member of a class, scoped to it. [S1] - **Enums in `switch`** β€” a natural fit for `switch` statements, matching each `case` against an enum member. [S1] - **Use case** β€” values known never to change: month names, weekdays, colors, card suits, etc. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Basic declaration + access: `enum Level { Low, Medium, High } ... Level myVar = Level.Medium; Console.WriteLine(myVar);` β†’ `"Medium"`. [S1] - Enum inside a class: `class Program { enum Level { Low, Medium, High } static void Main(string[] args) { Level myVar = Level.Medium; Console.WriteLine(myVar); } }` β†’ `"Medium"`. [S1] - Default numbering + explicit cast: `enum Months { January, February, March, April, May, June, July } ... int myNum = (int) Months.April;` β†’ `3` (0-indexed). [S1] - Custom starting value cascading: `enum Months { January, February, March=6, April, May, June, July } ... int myNum = (int) Months.April;` β†’ `7` (March=6 shifts April to 7, not the default 3). [S1] - Switch on enum: `switch(myVar) { case Level.Low: ...; break; case Level.Medium: Console.WriteLine("Medium level"); break; case Level.High: ...; break; }` β†’ `"Medium level"`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **enum을 좜λ ₯ν•˜λ©΄ 이름이 λ‚˜μ˜΄(C++와 닀름)**: C++μ—μ„œ enum을 `cout <<`으둜 좜λ ₯ν•˜λ©΄ 기본적으둜 μ •μˆ˜κ°’μ΄ λ‚˜μ˜€μ§€λ§Œ(별도 λ³€ν™˜ μ—†μ΄λŠ” 이름이 좜λ ₯λ˜μ§€ μ•ŠμŒ), C#은 `Console.WriteLine(myVar)`κ°€ κ³§λ°”λ‘œ 심볼릭 이름("Medium")을 좜λ ₯ν•œλ‹€λŠ” 점이 확인됨 β€” C#의 enum이 기본적으둜 λ¬Έμžμ—΄ ν‘œν˜„μ„ λ‚΄μž₯ν•˜κ³  μžˆμŒμ„ λ³΄μ—¬μ€Œ. [S1] - **μžλ™ μ¦κ°€Β·μˆ˜λ™ μž¬μ§€μ • 방식은 C++의 enumκ³Ό 동일**: 첫 멀버가 0λΆ€ν„° μ‹œμž‘ν•΄ μžλ™ μ¦κ°€ν•˜κ³ , νŠΉμ • 멀버에 값을 μˆ˜λ™μœΌλ‘œ μ§€μ •ν•˜λ©΄ κ·Έ 이후 멀버듀이 κ·Έ 값을 κΈ°μ€€μœΌλ‘œ λ‹€μ‹œ μ¦κ°€ν•œλ‹€λŠ” κ·œμΉ™μ΄ `[[CPP Enum]]`μ—μ„œ ν™•μΈλœ C++ enum κ·œμΉ™κ³Ό 차이가 μ—†λ‹€λŠ” 점이 확인됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) Level enum(Low/Medium/High)을 switchλ¬Έμ—μ„œ 각 case와 λ§€μΉ­μ‹œμΌœ ν•΄λ‹Ή 레벨 λ©”μ‹œμ§€λ₯Ό 좜λ ₯ν•˜λŠ” μ˜ˆμ œκ°€ μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Printing an enum prints its name; explicit cast needed for the integer value (C#): ```csharp enum Level { Low, Medium, High } Level myVar = Level.Medium; Console.WriteLine(myVar); // "Medium" -- name, not a number int myNum = (int) Level.Medium; // explicit cast required Console.WriteLine(myNum); // 1 ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.84 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C# Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CSharp Interface Multi]], [[CSharp Switch]], [[CSharp User Input]], [[CPP Enum]] - **μ°Έμ‘° λ§₯락:** Enums μ„Ήμ…˜μ˜ 유일 챕터 β€” User Input μ„Ήμ…˜μœΌλ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C# Enum β€” https://www.w3schools.com/cs/cs_enums.php ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C# Enum" page (Astra wiki-curation, P-Reinforce v3.1 format).