.NET 9 is here, and it's got some seriously nice coding upgrades. Have you heard about feature switches? They’re a way to toggle parts of your application on and off like a freaking light switch. But here's the kicker: it can actually make your code smaller. You heard that right. Smaller. Like, "still fits in your pants after a month of coding and pizza" smaller.
Think of feature switches as "if" statements on steroids. They let you enable or disable features in your app, which is awesome for testing out experimental stuff or deploying different versions without rewriting half your codebase. .NET 9's new attribute model makes this whole process really smooth and also plays nicely with .NET's built-in code trimming.
.NET 9 introduces two new attributes that are about to become your new best friends:
public class Feature { [FeatureSwitchDefinition("Feature.IsSupported")] internal static bool IsSupported => AppContext.TryGetSwitch("Feature.IsSupported", out bool isEnabled) ? isEnabled : true; internal static void Implementation() { // Feature-specific implementation } } if (Feature.IsSupported) Feature.Implementation();
public class Feature { [FeatureGuard(typeof(RequiresDynamicCodeAttribute))] internal static bool IsSupported => RuntimeFeature.IsDynamicCodeSupported; [RequiresDynamicCode("Feature requires dynamic code support.")] internal static void Implementation() { // Uses dynamic code } } if (Feature.IsSupported) Feature.Implementation();
.NET 9's new attribute model for feature switches is a sweet feature. With FeatureSwitchDefinitionAttribute and FeatureGuardAttribute, you can create apps that are smaller, faster, and more flexible than ever before. So go forth and trim that fat! (But seriously, keep eating pizza. We need fuel.)
*This is the first time I’ve been able to insert a Kraken reference so sue me. #seakraken