Parse enums and validate flags in modern C#
Enum.TryParse answers whether text can be converted to an enum representation. That is not the same as deciding whether the resulting value is allowed by your application. Numeric text can parse successfully even when no named member has that number.
Use a separate validation rule for a closed set of named states and for a set of combinable flags. Modern .NET already provides parsing APIs, so current code does not need a .NET 3.5 compatibility parser.
Parse and validate two different domains
Use the .NET 10 SDK. Create a console project in a new directory, then replace Program.cs with the example below.
dotnet new console --framework net10.0
dotnet run
Run dotnet run after saving the replacement. No additional packages are required.
using System;
foreach (var text in new[] { "ready", "2", "99", "missing", "" }) {
var valid = Enum.TryParse<State>(text, ignoreCase: true, out var state) && Enum.IsDefined(state);
Console.WriteLine($"State '{text}': {(valid ? state.ToString() : "rejected")}");
}
const Access allowed = Access.Read | Access.Write;
foreach (var text in new[] { "Read, Write", "3", "8", "0", "read" }) {
var parsed = Enum.TryParse<Access>(text, ignoreCase: true, out var access);
var valid = parsed && (access & ~allowed) == 0;
Console.WriteLine($"Access '{text}': {(valid ? ((int)access).ToString() : "rejected")}");
}
public enum State {
Unknown = 0,
Ready = 1,
Complete = 2
}
[Flags]
public enum Access {
None = 0,
Read = 1,
Write = 2
}
Expected output:
State 'ready': Ready
State '2': Complete
State '99': rejected
State 'missing': rejected
State '': rejected
Access 'Read, Write': 3
Access '3': 3
Access '8': rejected
Access '0': 0
Access 'read': 1
The state parser deliberately accepts names without case and numeric values that correspond to declared members. It rejects undefined 99. If your protocol accepts names only, use an explicit mapping or name validation before conversion. IsDefined alone does not reject the numeric spelling of a defined value.
Enum.TryParse
documents this numeric behavior.
Validate states and flag masks differently
Enum.IsDefined checks whether a value corresponds to a declared constant. It suits this closed state set. A valid combination of flags need not have its own declared name, so applying IsDefined to Read | Write would reject the valid combination in this example.
Instead, (access & ~allowed) == 0 verifies that no bits outside the permitted mask are set. The mask defines an application policy rather than simply trusting every value the enum's underlying integer can represent. Here it permits both individual flags and their combination.
FlagsAttribute guidance
explains why independent flags use powers of two.
Zero is accepted deliberately as None. If an operation requires at least one permission, add access != Access.None. If a combination is forbidden even though each bit is valid independently, add a rule for that combination. A bit-mask check cannot express all business constraints.
Keep conversion failures separate
Always inspect the Boolean result of TryParse. On failure the output value is the enum default, which can itself be a valid member. Looking only at the output value cannot tell you whether conversion succeeded.
The example uses a signed int underlying type with two small positive flags. The explicit mask rejects unexpected bits, including bits introduced by a negative parsed value. If your enum uses another underlying type, retain arithmetic in that enum's bit width instead of converting everything through int and truncating it.
Define the wire format
Case-insensitive names can be convenient for human input, while a public protocol may require stable numeric codes or an explicit string mapping. Renaming an enum member changes its default name representation. Do not assume a refactor is harmless when clients persist or transmit those names.
Test empty text, unknown names, differently cased names, defined and undefined numbers, zero and each permitted or forbidden flag combination. The [Flags] attribute communicates intended bit-field use. It does not perform authorization or automatically enforce the application's accepted values.
Source and further reading
This article revisits the problem raised in the following original posts. The explanation and examples here are newly written for .NET 10. A linked question supplies the motivation, rather than an author-attributed solution.
-
Herman Schoenfeld’s answer
on “Implementation of Enum.TryParse in .NET 3.5”, licensed under
CC BY-SA 3.0
.
-
Herman Schoenfeld’s question
on “Implementation of Enum.TryParse in .NET 3.5”, licensed under
CC BY-SA 3.0
.



Author: Herman Schoenfeld