C# Feature Flags
Overview
Feature flags (aka feature toggles) are a technique for enabling or disabling functionality in a system without deploying new code. They decouple feature release from code deployment by wrapping new or experimental logic in conditional checks.
"Feature flags allow us to ship code to production early and safely control its visibility to users, teams, or environments through configuration."
They’re widely used in continuous delivery, A/B testing, canary releases, beta features, and gradual rollouts.
Quick Description
"Feature flags allow us to deploy code continuously while controlling feature exposure via config. We wrap features in conditional logic so we can enable or disable them without modifying or redeploying the application. It supports safer releases, experimentation, and rollback strategies — and in .NET, it integrates easily via config files or flag management libraries."
Example: Feature Flags in C#
1. Simple AppSettings-Based Flag
// appsettings.json
{
"FeatureFlags": {
"EnableNewCheckout": true
}
}
// Feature flag check in service/controller
public class CheckoutService
{
private readonly IConfiguration _config;
public CheckoutService(IConfiguration config)
{
_config = config;
}
public void ProcessCheckout()
{
bool isEnabled = _config.GetValue<bool>("FeatureFlags:EnableNewCheckout");
if (isEnabled)
{
// New feature logic
RunNewCheckoutFlow();
}
else
{
// Old logic
RunLegacyCheckout();
}
}
}
2. Strongly-Typed Flag Options with IOptions<T>
public class FeatureFlags
{
public bool EnableNewCheckout { get; set; }
}
// Program.cs
builder.Services.Configure<FeatureFlags>(builder.Configuration.GetSection("FeatureFlags"));
public class CheckoutService
{
private readonly FeatureFlags _flags;
public CheckoutService(IOptions<FeatureFlags> options)
{
_flags = options.Value;
}
public void ProcessCheckout()
{
if (_flags.EnableNewCheckout)
RunNewCheckoutFlow();
else
RunLegacyCheckout();
}
}
Benefits of Using Feature Flags
Benefit | Description |
---|---|
Decouple release from deploy | Push code to production but release later |
Roll back quickly | Turn off a feature without redeploying |
Targeted releases | Enable for specific users or environments |
Support experimentation | A/B testing, beta features, etc. |
Safer deployments | Gradual rollout, monitor before full exposure |
Drawbacks and Considerations
Drawback | Mitigation |
---|---|
Code clutter* | Isolate with clear naming or abstraction |
Flag rot | Schedule cleanup of unused flags |
Test complexity | Ensure tests cover both flag states |
Security risks | Don’t use flags for access control unless enforced server-side |
Tools & Libraries
Tool | Description |
---|---|
LaunchDarkly | Enterprise feature flag service |
Azure App Configuration | Feature management as a service |
Microsoft.FeatureManagement | Lightweight, open-source NuGet package for flags |
Unleash | Open-source feature toggle platform |
When to Use Feature Flags
Scenario | Use Feature Flag? | Why |
---|---|---|
Gradual rollout of new UI | ✅ Yes | Control release by audience |
Experimental logic or A/B testing | ✅ Yes | Test without impact |
Sensitive logic changes | ✅ Yes | Roll back instantly if needed |
Clean-up of legacy features | ❌ No | Remove via versioning instead |
Business-level config (non-code) | ✅ Yes | Empower non-devs to toggle |
Summary for Interviews
"Feature flags give teams the ability to control feature exposure at runtime, enabling safer deployments, faster feedback, and gradual rollouts. I’ve used them in C# via
appsettings.json
,IOptions<T>
, and Microsoft’s feature management NuGet package. They’re essential in any mature CI/CD pipeline — especially when building resilient, testable, user-focused applications."