[Proposal]: calling static generic method in generic class without the generic type of the class like java #4375
-
FEATURE_NAME
SummaryA small grammer improvement. It doesn't have any effect to fill a type in a generic class when calling its' generic static method since the method is generic itself(class generic type doesn't equal method generic type or else it will raise warning CS0693). Calling the method without specifiing a type for the class should be legal Motivationmore elegant grammer Detailed designsee the calling of "MethodA", it is very easy to understand this case.
also note the case in "using static"
these two pieces of code run normally in my computer. DrawbacksJava has this feature and it is definitely not harmful AlternativesDon't fix it and specify the type infered by the method. But it dosen't have any effect on program and it maybe confusing when someone specify a type that doesn't equal with the infered type. Unresolved questionsDesign meetings |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Java doesn't have generics at runtime and so all static methods on a generic class are actually the same. In C# this is not the case - theoretically a different method is compiled for every type parameter used in static method (ignoring done optimisations to share generated code) and so the type parameter must be set. E.g, this is legal in c#: static class A<T> where T : struct
{
static object New() => new T();
}
A.New(); //what would this return? |
Beta Was this translation helpful? Give feedback.
Java doesn't have generics at runtime and so all static methods on a generic class are actually the same. In C# this is not the case - theoretically a different method is compiled for every type parameter used in static method (ignoring done optimisations to share generated code) and so the type parameter must be set.
E.g, this is legal in c#: