C# Enums
List a fixed set of names with enum so the program picks from that list only. Write public enum Level with Low, Medium, and High. Inside Pine.Main, assign Level mark = Level.Medium; and print mark. Run with dotnet run. The screen should show Medium. A second demo runs a switch on the same mark, with case Level.Medium:, and prints medium. Leave abstract classes for the next page. This lesson is a short list of fixed names, and how you print or branch on one of them.
Print one enum name
The interfaces page filled empty method promises. Signal named Horn, and Truck : Signal supplied the body. This page lists names instead of methods. Open the Pine file. The starting class name stays Pine. Delete leftover Signal and Mark demos if they bury the enum story, leftover Horn prints of honk, leftover Path demos, and any ReadLine line. The program must not wait for typing. Keep using System; at the top.
Declare the enum above Pine:
public enum Level
Inside the braces, list three names. Separate them with commas. The last name needs no comma after it:
Low,
Medium,
High
Spell each name with a capital first letter. That is the C# habit for enum names. Java often writes the same idea in all capitals, such as MEDIUM. Do not copy that shape into this file. The teaching names are Low, Medium, and High.
Inside Main, pick one name and print it:
Level mark = Level.Medium;
Console.WriteLine(mark);
You do not write new Level(). You write the type name, a dot, and the listed name: Level.Medium.
using System;
public enum Level
{
Low,
Medium,
High
}
public class Pine
{
public static void Main()
{
Level mark = Level.Medium;
Console.WriteLine(mark);
}
}
Save, then run with dotnet run. The screen shows Medium. Walk the run once. C# starts at Pine.Main. The variable mark holds the name Level.Medium. Printing mark writes that name. The other names Low and High stay available on the type, but this demo only prints the one you assigned.
Read the assignment once more. The type on the left is Level. The value on the right is Level.Medium. Both sides belong to the same enum. You are not storing a free piece of text. You are picking a name that the enum already lists. If you later change the assigned name to Level.Low or Level.High in a private experiment, only names from that list are legal. A string such as "Medium" is not a Level, and the build refuses to store it in mark.
If you still see honk from an interface file, switch to the enum file and run again. If the build says it cannot find Medium, check the spelling and the capital M. If you invent a fourth name at the call site without listing it inside the enum, the build refuses. The list in the enum is the allowed set.
Say the rule out loud while you watch the screen: the enum lists the names, the code picks one with a dot, and the print shows that name. Do not invent an abstract class on this page. Abstract classes wait for the next lesson.
C# lets public enum Level sit in the same file as public class Pine. You do not have to hide the enum to satisfy a one-public-type rule. That rule belongs to Java files. Keep both types public in this teaching file.
Branch on the enum with switch
A second program keeps the same Level list. Main still assigns Level.Medium. A switch on mark prints a lowercase word for the chosen case.
using System;
public enum Level
{
Low,
Medium,
High
}
public class Pine
{
public static void Main()
{
Level mark = Level.Medium;
switch (mark)
{
case Level.Low:
Console.WriteLine("low");
break;
case Level.Medium:
Console.WriteLine("medium");
break;
case Level.High:
Console.WriteLine("high");
break;
}
}
}
Run again with dotnet run. The screen shows medium. The switch entered case Level.Medium: because mark held Level.Medium. The break ends that case so the later cases do not run. Change only the assigned name in a later experiment if you want to see low or high. Keep the enum list the same.
Read each case label carefully. C# wants the type name on the label: case Level.Medium:. A bare case Medium: does not build. Java switch labels often omit the type name. Do not copy that shape into this file. The three labels are case Level.Low:, case Level.Medium:, and case Level.High:.
Each case that prints must end with break. If you delete break after a print, the build refuses. C# does not fall from one working case into the next the way a Java switch can. The break is how this teaching file keeps the output to one clear line. Walk the three cases with your eyes once. Level.Low maps to low. Level.Medium maps to medium. Level.High maps to high. Only one of those prints runs for a single assignment.
If leftover interface demos still sit nearby, keep them as history. For this lesson, run the enum file so the screen shows Medium or medium, not honk.
📊 Level enum names
| Name | Role on this page |
|---|---|
| Low | listed name; switch can print low |
| Medium | assigned and printed in both demos |
| High | listed name; switch can print high |
The three Level names
The table is the short map of this page. The enum holds all three names. The first demo prints the middle name as Medium. The second demo maps that same choice to the lowercase word medium. Keep the list short and fixed. Do not treat mark as a free string on this page. The first column is the name inside the enum. The second column is the job that name does in these two demos.
Compared with interfaces
On the interfaces page, Signal promised a method and Truck : Signal filled Horn so the screen showed honk. On this page, enum Level lists names and Pine picks one. You still run with dotnet run. The new pieces are enum, the capital-letter name list, Level.Medium, and, in the second demo, switch cases written as case Level.Medium: with a break after each print.
If a leftover interface file is still the one you run, you will see interface, a colon, and honk. Move to the enum file so the top shows public enum Level and Main assigns a listed name. Keep abstract-class demos off this page. Keep keyboard input off this page.
Before abstract classes
You can declare public enum Level with Low, Medium, and High, assign Level.Medium, and see Medium on the screen. You can switch on that mark, hit case Level.Medium:, and see medium. Run Pine with dotnet run for each kept demo in turn. The program picked from a fixed list. The next page is abstract classes, where a parent type can leave a method without a body for a child to finish.
Keep the print demo for a clear name on the screen. Keep the switch demo for a clear branch on that name. Keep abstract files off this page. Those three habits are the checkpoint before you move on. When Medium appears from a direct print, and when medium appears from the switch, enums did their job for this lesson, and you are ready for the next topic about abstract classes.
One last pass through the file: one Pine with Main, one public enum Level with three names, case labels that include Level., a break after each print, no keyboard wait, and no forced abstract class. Run dotnet run once more on the demo you keep open. If the screen still matches the middle row of the table, the enum list is solid.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top