Proposal: Access generic type methods and properties, without specifying generic parameter if possible #2912
-
In C# if a class is defined with generic parameters, all operations with it from other classes - access to properties etc - need to be done including the generic parameters:
At the moment to use any property of this class, we need to do:
but if the restriction can be relaxed, we can access non-generic dependent properties and methods like so:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Note that static fields in a generic class are seperate for every generic instantiation. public class Class<T>
{
public static int P { get; set; }
}
...
Class<int>.P = 1;
Class<string>.P = 2;
Console.WriteLine(Class<int>.P); //writes 1
Console.WriteLine(Class<string>.P); //writes 2 |
Beta Was this translation helpful? Give feedback.
-
You're making the assumption that a "free" property that doesn't use the type parameter in its declaration is independent; I don't think that's true. public interface IDispatcher
{
Task SendMessage(Message message);
}
public class Demo<T>
{
public IDispatcher Dispatcher { get; }
} The actual implementation of If we allow omission of the type parameter only when the compiler can prove it's a "free" member, trivial changes to the implementation of the class (changes that have no impact on its external contract) become breaking changes. Plus a couple of nitpicks ...
|
Beta Was this translation helpful? Give feedback.
-
Var does not really solve the issue, as if I need to use the generic class as a parameter to a function or property of another class (what often happens is something needs to process the non-generic parts of the class as they often carry a common theme) one still needs to specify the generic parameter. As a solution an interface declaration can be used, or a non-generic base class, but these take quite a bit of extra code, so my idea/question was if another more compact solution is possible. The full case where this would be very useful is fairly lengthy to explain so I tried to simplified a bit. I do fully agree on the rest of the comments though, thank you! Indeed it seems like a small change in the implementation could break the "declaration" capabilities of the class, which sounds like a deal breaker, as it will be hard to understand why... unless additional keywords are introduced in the language to support it, for ex. ones that can mark a method explicitly as "not_using_generic_parameters". However one more idea here can maybe help solve this - what if the language support simply works on the dynamic level. Meaning it allows me to perform operations on an existing instance, even if I do not know what the instance generic parameters are. Something similar to what this does:
becomes a much safer
On runtime, the instance will already have a full type (incl. generic parameters) so the operation can be applied to it. |
Beta Was this translation helpful? Give feedback.
-
The variable doesn't have a full type. Open generics aren't valid type for variables. If you want to expose the non-generic parts of a generic, you must explicit define a base class or interface for it. |
Beta Was this translation helpful? Give feedback.
Note that static fields in a generic class are seperate for every generic instantiation.