C# 14 - Static methods in extension members with same name and parameter types #9537
-
I'm using C# 14 new extension members syntax, the documentation says we can extend static methods now. However, I found that if two static method with same name and parameter types but extend different types, they will conflict. public static class Extensions
{
extension(System.Windows.Media.Color color)
{
public static System.Windows.Media.Color FromHex(string hex) { /* implementation */ }
}
extension(System.Drawing.Color color)
{
public static System.Drawing.Color FromHex(string hex) { /* implementation */ }
}
} The two static methods have same name and parameter types, but they extend different types. It complain that "Type 'Extensions' already defines a member called 'FromHex' with the same parameter types". I think these two methods with the same name should be compiled into methods with different names, and can be automatically redirected to the corresponding methods when called from other places. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
They are intentionally compiled into plain methods to support explicit invocation and compatibility with traditional extension methods. |
Beta Was this translation helpful? Give feedback.
They are intentionally compiled into plain methods to support explicit invocation and compatibility with traditional extension methods.