[Proposal] implicit target-typed using static
#8174
-
I'd like to propose a feature that would, based on the target type, automatically allow one to omit the class name prefix when accessing static members. Here are a few examples: public void Tint(Color color)
{
// Do something here that is not relevant for the question
}
public enum Color
{
Red,
Green,
Blue,
} Normally, I can call the Tint(Color.Red); Or I can opt into adding a using static Color;
Tint(Red); I propose this becomes possible by default, without adding the Tint(Red); Based on the target-type and parameter position, the This would work similarly with other constructs such as:
// `Color` is omitted since it can be inferred by the type of the `color` variable
return color switch
{
Red => "Apple",
Green => "Lemon",
Blue => "Blueberry",
}
// `Color` is omitted since it can be inferred by the explicit type of the element
IList<Color> colors = [Red, Green, Blue];
// `CultureInfo` is omitted since it can be inferred by the type of the argument
var text = string.Format(InvariantCulture, "something {0}", arg);
string text = Empty; If a member with the same name as the I believe this would substantially simplify the language in the vast majority of cases and would be broadly applicable. The need for an explicit I mentioned this idea here: As it would also apply to interpolated literals when needing to pass the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
But why? What benefit does this serve if the desired outcome can already be achieved? |
Beta Was this translation helpful? Give feedback.
-
this is just a massive breaking change, so many codebases will break |
Beta Was this translation helpful? Give feedback.
-
Would this be covered by #2926? |
Beta Was this translation helpful? Give feedback.
Would this be covered by #2926?