C#

Comfort features for enums with Enums.NET

dotnetpro (de) Tam Hanna
C#

Tam Hanna walks through the Enums.NET NuGet package and what it adds to plain enums: reflection-free string conversion, attribute reading, bit flag helpers and value validation. A practical look for C# developers tired of hand-written glue code.

Key points

  • Version 4.x runs on almost any .NET version, while 5.x drops .NET 4.5 and .NET Standard 1.0.
  • Enum members get a `Symbol` attribute, read back through `GetAttributes().Get<SymbolAttribute>()`.
  • The `PrimaryEnumMember` attribute decides which of several members sharing a value wins.
  • `AsString()` replaces `ToString()` without touching reflection and caches the result after the first call.
  • `HasAnyFlags()` and `HasAllFlags()` check bit combinations without manual masking; `GetFlags()` enumerates the set bits.
  • `Enums.IsValid()` catches numeric values cast into an enum type that do not map to any member.

Real-world use

  • Using `[Flags]` enums to manage individual bits of hardware registers, as in the article's PIC16F84A GPIO example.
  • Binding enum members to short external codes or symbols with an attribute.
  • Validating that numeric values coming from users or a database map to a real enum member.

This summary was generated by the agent from the original post on dotnetpro (de). The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message