Codectionary / Developer documentation / C#

Validate Parsed Enum Values

Separate enum parsing from checking that a value is named and allowed by your application.

Read the syntax

Separate enum parsing from checking that a value is named and allowed by your application.

No State member is declared with value 99, so IsDefined returns false.

What it is and why to use it

An enum variable can hold an underlying numeric value even when no member has that number. Enum.TryParse can therefore succeed for numeric text that is not a declared member. Use a separate definition or allowed-value check when a form or configuration must name a recognised option. Parsing never establishes authorisation.

How it works

TryParse returns success and writes the resulting enum. IsDefined checks whether a single value is explicitly declared. For Flags enums, a valid combination of declared bits need not be a separately named member, so IsDefined is not a general flags-combination validator. Decide whether numeric text and case-insensitive names are permitted by the input format.

Common mistakes and limitations

Do not assume that TryParse success means a supported business state. Do not reject every unnamed Flags combination merely because IsDefined returns false. Prefer a deliberate allowlist when only some members are valid for a particular operation.

Run the examples locally

These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.

Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.

C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.

Create and enter a project

dotnet new console -n CSharpExample --framework net10.0
cd CSharpExample

CSharpExample.csproj settings used for these examples

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <LangVersion>12.0</LangVersion>
    <Nullable>enable</Nullable>
    <ImplicitUsings>disable</ImplicitUsings>
  </PropertyGroup>
</Project>

Build and run after saving one example in Program.cs

dotnet build
dotnet run --no-build

Continue learning

Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.

Syntax

Enum.TryParse<State>(text, out var state) && Enum.IsDefined(state)

Examples

Reject an unnamed numeric state

Observe successful numeric parsing followed by a failed definition check.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

bool parsed = Enum.TryParse<State>("99", out var state);
Console.WriteLine(parsed);
Console.WriteLine(Enum.IsDefined(state));
enum State { Draft = 0, Published = 1 }
  1. The enum’s underlying int can represent 99, so parsing succeeds.
  2. No State member is declared with value 99, so IsDefined returns false.
  3. A configuration validator can combine both checks instead of accepting every parseable integer.
  4. Expected output: True False
  5. Validate the allowed state after parsing.

Apply a narrow state policy

Accept a case-insensitive member name only when it is defined and permitted.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

string text = "published";
bool allowed = Enum.TryParse<State>(text, true, out var state)
    && Enum.IsDefined(state) && state == State.Published;
Console.WriteLine(allowed);
enum State { Draft, Published, Archived }
  1. The true argument selects case-insensitive name parsing.
  2. The definition check rules out unnamed numeric values.
  3. The final comparison applies this operation’s specific policy. Other named members can still be rejected by a business rule.
  4. Expected output: True
  5. Validate the allowed state after parsing.

Best practices

  • Validate the allowed state after parsing.
  • Use flag-specific rules for combinations rather than a single-member check.

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading