## Pattern Matching #### List and similar: This is null safe for `Items` in this example. 40x Faster than using `?.Any()` in a microbenchmark when Items is not null but could be empty. In cases where the list will typically be null, `?.Any()` measured about 10x faster, but these are all sub-nanosecond measurements and neither has any allocations. ```csharp public class Foo { public List? Items { get; set; } public void ProcessItems() { if (Items is { Count: > 0 }) { // do stuff } } } ``` ## Microsoft Dependency Injection #### Registering a named HTTP client using config: ```csharp // or another kind of builder ... var builder = WebApplication.CreateBuilder(args); builder.Services.AddHttpClient("MyNamedHttpClient", static (services, httpClient) => { // this method is run when the client is resolved. // resolves the config from the services collection var config = services.GetService(); // configures the client httpClient.BaseAddress = new Uri(config.GetValue("MyNamedHttpClientBaseAddress")); httpClient.DefaultRequestHeaders.Add(HeaderNames.Accept, "application/json"); }); ``` #### Using an HTTP client registered by name: Use constructor injection or some other technique to resolve an `IHttpClientFactory`. Then resolve the named client like this: ```csharp var client = httpClientFactory.CreateClient("MyNamedHttpClient"); ``` #### General-purpose DI container (for console app, or tests, etc) ```csharp var containerBuilder = Host.CreateApplicationBuilder(); // register a singleton to represent a specific interface. (the interface is optional) containerBuilder.Services.AddSingleton(new WhateverClass()); containerBuilder.Services.AddTransient(); containerBuilder.Services.AddSingleton(x => { /* x is an IServiceProvider and has .GetRequiredService etc */ }); var container = containerBuilder.Build(); var myThing = container.Services.GetRequiredService(); ```