Open
Description
Target-typed static member lookup
- Specification: https://github.com/dotnet/csharplang/blob/main/proposals/target-typed-static-member-lookup.md
- Discussion: Proposal discussion: Target-typed static member lookup #9078
Summary
This feature enables a type name to be omitted from static member access when it is the same as the target type.
This reduces construction and consumption verbosity for factory methods, nested derived types, enum values, constants, singletons, and other static members. In doing so, the way is also paved for discriminated unions to benefit from the same concise construction and consumption syntaxes.
type.GetMethod("Name", .Public | .Instance | .DeclaredOnly); // BindingFlags.Public | ...
control.ForeColor = .Red; // Color.Red
entity.InvoiceDate = .Today; // DateTime.Today
ReadJsonDocument(.Parse(stream)); // JsonDocument.Parse
// Production (static members on Option<int>)
Option<int> option = condition ? .None : .Some(42);
// Production (nested derived types)
CustomResult result = condition ? new .Success(42) : new .Error("message");
// Consumption (nested derived types)
return result switch
{
.Success(var val) => val,
.Error => defaultVal,
};