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]
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#):
enumLevel{Low,Medium,High}LevelmyVar=Level.Medium;Console.WriteLine(myVar);// "Medium" -- name, not a numberintmyNum=(int)Level.Medium;// explicit cast requiredConsole.WriteLine(myNum);// 1
✅ 검증 상태 및 신뢰도
상태: draft
검증 단계: conceptual
출처 신뢰도: B (W3Schools — widely used educational reference, not a primary standards body)