This solution provides comprehensive examples of working with enums in C#, covering basic usage, parsing, equality checks, flag enums with bitwise operations, and different patterns for handling enum values in application logic. The main project defines a Product enum representing courses, subscriptions, and e-books, along with a ProductProcessor class that demonstrates three approaches to handling enum-based logic.
The three handling patterns compared are: if/else chains, switch statements, and dictionary-based mapping. The dictionary mapping approach is highlighted as the preferred pattern for extensibility and maintainability. The solution also includes a TestsToEnsureNoChange project with unit tests and a WhatElseIfNotEnums project exploring alternative approaches to enums. This is a practical reference for understanding enum best practices in C#.
- .NET 7.0 SDK or later
cd EnumExamples
dotnet runIf you found this useful and you want to learn more about C#, .NET, and software engineering, subscribe to the free Dev Leader Weekly newsletter:
Subscribe to Dev Leader Weekly
- All Links
- Website - Dev Leader
- YouTube - Dev Leader
- YouTube - Dev Leader Path To Tech
- YouTube - Dev Leader Podcast
- YouTube - CodeCommute
- Newsletter - Dev Leader Weekly
- LinkedIn - Nick Cosentino
- GitHub - ncosentino
- Twitter/X - Dev Leader
- Threads - Dev Leader
- Bluesky - Dev Leader
- Mastodon - Dev Leader
- Facebook - Dev Leader
- TikTok - Dev Leader
- Twitch - Dev Leader
- Stack Overflow - Nick Cosentino
Powered by BrandGhost 👻