You’re Using Enums Wrong – Dev Leader Weekly 16

Hey there!

I wanted to start off by getting your feedback so I can make this newsletter valuable for *you*, my subscribers! Please take a quick second to answer:

Remember, you can refer your friends to sign up for my newsletter! You can get free membership access for a few months to my site to access the newsletter archive and my Behind The Screen weekly vlogs that I do. Check out the details at the end of this issue.

You’re Using Enums Wrong

A Quick Primer On Enums

If you’re not familiar with Enums in C#, we should probably get that cleared up before diving any deeper! Enums allow us to create a type in C# that feels like a blend of strings and integers. Enums hold a numeric value, but they also have a name associated with them. Here’s a brief example:

public enum WeekDays
    Monday, // this has a value of 0
    Tuesday, // this has a value of 1
    Wednesday, // this has a value of 2
    Thursday, // this has a value of 3
    Friday, // this has a value of 4

This allows us to write code where we can access WeekDays.Thursday and work with effectively a numeric value with a readable name for developers. This is more practical than using the string “Thursday” everywhere because the Enum is essentially a numeric value. But this Enum is also more expressive than just a constant numeric value! Seems like a win!

Before we touch on some more advanced topics, here’s a video on getting started with enums:

YouTube player

Why You’re Using Enums Wrong

To be clear – while the title of this piece is a bit click-baity, I’m wagering at least most people fit into this group. And hopefully you know me by now – I don’t believe in absolutes. So “wrong” is too strong of a word here, but I am going to explain why I think you might be making more work for yourself.

Enums are super convenient. As C# programmers, they give us a big boost to readable and understandable code because they’re expressive. Instead of comparing numbers slapped into some constants — or better yet, just left as magic numbers maybe with a comment or two as the cherry on top — Enums allow us, as developers, to know the meaning. They’re readable. They have names. It’s great stuff.

Where we start to misuse them is representing data that is not finite or fixed. And when you couple that with code that is consuming or working with the Enums in multiple places… here comes your fun time with maintenance.

If your set of values in your Enum is changing frequently, multiply your maintenance cost by all of the spots that are consuming those Enum values. All of those if/else or switch statements that are operating on the Enum ranges? Yeah, those need to be maintained now. And even scarier – You’re serializing the enum values out to some persistent storage like a database… Guess what? Someone just inserted a new value into your range so “3” is no longer Products.NicksCoolProduct (or someone removes an enum value so “NicksCoolProduct” can’t be parsed from your DB record).

The Path Forward

So here’s the deal. It’s not “wrong” to use Enums this way, it’s just that you’re making work for yourself. I highly recommend that you use Enums for finite values (days of the week, months of the year, etc…) or for sets of values that will change very infrequently (maybe a finite state machine with well-understood behavior/states). You can also aim to push Enum consumption into fewer spots so you’re reducing that maintenance multiplier I mentioned above.

In summary – there’s no right or wrong here. But if you consider these tips, then I think you can make it easier on yourself to maintain codebases with Enums. And if you watch this video on enum usage, I hope that it’ll help explain this clearly:

YouTube player

Thought-Provoking Quote of the Week:

To Var or Not To Var - Dev Leader Quote

Readability… readability… readability. We know it’s top of mind as programmers because every single person on social media is constantly reminding us! And for good reason too — It’s super important to reduce cognitive load when programming.

But we have this fun polarizing keyword in C# called var. Most of us are probably familiar with it, especially because similar constructs are available in other languages. On the surface, the value of var is that we can shorten some verbose lines:

Dictionary<string, object> nicksCoolDictionary = new Dictionary<string, object>();
var nicksCoolDictionary = new Dictionary<string, object>();

We can see that we trimmed down the declaration with assignment line length pretty significantly and reduced the redundant type information. We know it’s a dictionary of objects keyed by strings, so why do we express that twice? But one of the key arguments against var is for situations like this:

var result = SomeCoolMethod();

When reading code, there’s now a “tax” to understand what the type of result is. If you’re using code review tools or systems, you might need to go look up the return type of the method being called. If you’re in Visual Studio, it’s of course simple to just hover over the method to see the full signature — but the argument is that it’s an extra step to know the type.

Personally, I’ve always found that reducing the redundant type information has never interfered with my understanding. It’s also helped with preventing breaking changes when refactoring return types and actually made maintenance less effort. The more succinct lines are a great value add for me.

While I still use var in the second example I provided, I’ve more recently switched to making my types in my declarations as follows:

Dictionary<string, int> someDictionary = new();

This moves the type information to the left and helps some of the folks that hate seeing that special keyword popup. But the key takeaway here is that come up with an agreement on readability and standards… and try to be consistent as much as it makes sense to.

Featured Content:

How To Convert Strings To Enums in C#

YouTube player

This is one of the other videos in the C# Enums series. The article at the top lists two others and there’s yet another one currently hidden at the end of this one. That’s TWO early-access videos for subscribing! Aren’t you lucky!

Blazor Unit Testing Tutorial – How to Master with Practical Examples in C#

Blazor Unit Testing Tutorial - How to Master with Practical Examples in C#

Building on more examples from previous weeks on how we can write unit tests for our Blazor applications!

How to Generate Endless Ideas for Programming Projects

How to Generate Endless Ideas for Programming Projects in C# and .NET

Look — I know what you’re thinking. That’s a dumb face. I know, I know. But statistically, people click this stuff more. I don’t want you to be a statistic though! I want you to click this because you want to learn about different ways you can get your own ideas for making practice applications for learning how to program. The less time you spend generating ideas for apps, the more time you spend coding and learning.

Beginner’s CSharp Switch Statement Tutorial: How To Properly Use A Switch Statement

Beginner's CSharp Switch Statement Tutorial - How To Properly Use A Switch Statement

If you’re looking to understand the basics of using switch statements then this will be a perfect beginner article for you. There are no very advanced techniques covered here, but this should help familiarize you with use cases and syntax!

ASP.NET Vertical Slice Project Template – How To Start Easily

ASP.NET Vertical Slice Project Template - How To Start Easily

Piggy-backing from last week’s newsletter, here’s a similar take on using the project template for vertical slices. If you didn’t see last week’s newsletter, this might be a useful read!


These are products & services that I trust, use, and love. I get a kickback if you decide to use my links. There’s no pressure, but I only promote things that I like to use!

      • RackNerd: Cheap VPS hosting options that I love for low-resource usage!
      • Contabo: Alternative VPS hosting options with very affordable prices!
      • ConvertKit: This is the platform that I use for my newsletter!
      • SparkLoop: This service helps me add different value to my newsletter!
      • Opus Clip: This is what I use for help creating my short-form videos!
      • Newegg: For all sorts of computer components!
      • Bulk Supplements: For an enormous selection of health supplements!
      • Quora: I try to answer questions on Quora when folks request them of me!

    Solution to Last Week’s Coding Challenge:

    Did you try out last week’s coding challenge? This is a pretty common variation of a type of problem that you could get in an interview or come across doing LeetCode! You can find a working solution here that can be run right in the browser.

    This Week’s Coding Challenge:

    Coding Challenge: Build a Personal Diary App with ASP.NET Core

    Develop a minimalistic personal diary web application using ASP.NET Core (maybe you want to try Blazor out for this!) where users can jot down their daily thoughts. Try implementing some or all of the following features:

    1. User Registration and Login: Allow users to register and authenticate. Secure user credentials appropriately.
    2. Diary Entry Management: Users should be able to:
      • Create a new diary entry with a title, content, and date.
      • View a list of their past entries, displayed in reverse chronological order.
      • Edit an existing entry.
      • Delete an entry.
    3. Data Persistence: Store the diary entries in a local database using Entity Framework Core.

    Bonus Points:

    As always, thanks so much for your support! I hope you enjoyed this issue, and I’ll see you next week.

    ​Nick “Dev Leader” Cosentino
    [email protected]

    Dev Leader YouTube
    Follow on LinkedIn
    Dev Leader Instagram

    P.S. If you enjoyed this newsletter, consider sharing it with your fellow developers. Let’s grow our community together!

    author avatar
    Nick Cosentino Principal Software Engineering Manager
    Principal Software Engineering Manager at Microsoft. Views are my own.