dotnet
configuration
enum
case-insensitive
parsing

.net Custom Configuration How to case insensitive parse an enum ConfigurationProperty

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

When you build a classic System.Configuration custom section in .NET, enum values often come from XML attributes. If you want those values to parse case-insensitively, the safest approach is to use a custom converter that calls Enum.Parse or Enum.TryParse with ignoreCase: true. That keeps the configuration API strongly typed while avoiding fragile casing requirements in the config file.

Why the Default Setup Can Be Painful

A typical custom configuration section exposes an enum property like this:

csharp
1public enum LogMode
2{
3    Off,
4    Console,
5    File
6}

And the section might read from XML such as:

xml
<mySection mode="console" />

If your parsing path expects exact enum casing, console may fail while Console succeeds. That is unnecessary friction for configuration files.

Use a Custom ConfigurationConverterBase

Create a converter that understands your enum and parses it case-insensitively.

csharp
1using System;
2using System.Configuration;
3using System.Globalization;
4
5public class LogModeConverter : ConfigurationConverterBase
6{
7    public override object ConvertFrom(ITypeDescriptorContext ctx, CultureInfo culture, object data)
8    {
9        if (data is string text && Enum.TryParse(typeof(LogMode), text, true, out var result))
10        {
11            return result;
12        }
13
14        throw new ConfigurationErrorsException($"Invalid LogMode value: {data}");
15    }
16
17    public override object ConvertTo(ITypeDescriptorContext ctx, CultureInfo culture, object value, Type destinationType)
18    {
19        return value?.ToString() ?? string.Empty;
20    }
21}

The important part is the third argument to Enum.TryParse: true means ignore case.

Apply the Converter to the Property

Now attach the converter to your configuration property.

csharp
1using System.Configuration;
2using System.ComponentModel;
3
4public class MySection : ConfigurationSection
5{
6    [ConfigurationProperty("mode", IsRequired = true)]
7    [TypeConverter(typeof(LogModeConverter))]
8    public LogMode Mode
9    {
10        get => (LogMode)this["mode"];
11        set => this["mode"] = value;
12    }
13}

With that setup, all of these can map to the same enum value:

  • 'Console'
  • 'console'
  • 'CONSOLE'

Example Config File

xml
1<configuration>
2  <configSections>
3    <section name="mySection" type="MyNamespace.MySection, MyAssembly" />
4  </configSections>
5
6  <mySection mode="console" />
7</configuration>

And the read path stays strongly typed:

csharp
var section = (MySection)ConfigurationManager.GetSection("mySection");
Console.WriteLine(section.Mode);

An Alternative: Read a String and Parse Manually

If you want the least magic, you can store the raw configuration value as a string and parse it in a separate property.

csharp
1[ConfigurationProperty("mode", IsRequired = true)]
2public string ModeText
3{
4    get => (string)this["mode"];
5}
6
7public LogMode Mode => Enum.Parse<LogMode>(ModeText, ignoreCase: true);

This is simpler to reason about, but you lose some of the elegance of a directly typed configuration property.

Validate Early and Fail Clearly

Whichever approach you choose, fail with a precise configuration error when the value is invalid. Configuration bugs are much cheaper to diagnose when the startup error says exactly which property and value failed, rather than surfacing later as a null or default-path behavior elsewhere in the application.

Keep the Advice Scoped to Classic Configuration

This pattern applies to the older System.Configuration model used by ConfigurationSection and ConfigurationProperty. If you are on modern Microsoft.Extensions.Configuration, the binding story is different. Mixing those two configuration systems is a common source of confusion when searching for examples.

Common Pitfalls

  • Assuming enum parsing will always be case-insensitive by default.
  • Throwing generic exceptions instead of a configuration-specific error.
  • Hiding invalid values by silently falling back to a default without logging.
  • Overcomplicating the section when a plain string plus explicit parse would be enough.
  • Mixing Microsoft.Extensions.Configuration advice with classic System.Configuration code.

Summary

  • Use a custom ConfigurationConverterBase for case-insensitive enum parsing in classic .NET configuration sections.
  • 'Enum.TryParse(..., true, ...) is the key implementation detail.'
  • Attach the converter with TypeConverter on the property.
  • A string-backed property with manual parsing is a simpler alternative.
  • Keep the error message clear so invalid config values are easy to diagnose.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.