Suggestions for Enhancing C# Interfaces #8040
Replies: 7 comments
-
A lot of times, we don't want some properties to be exposed, but for the implementation of the default interface, we need it, so what we really need is a private member declaration |
Beta Was this translation helpful? Give feedback.
-
However, many times the implementing class may not want to have these private fields from the interface. At this time, a choice can be considered. That is, if the implementing class explicitly declares these private fields declared from the interface, it indicates that all those related default implementations of the interface are valid for this class, otherwise these default implementations are invalid. This means that the implementing class has two choices. One is to explicitly implement the private fields from the interface, then it can use all related default methods of the interface. Otherwise, this class does not have these private fields of the interface and is equivalent to the previous situation, that is, the class only accepts the constraints of the interface on public methods and traditional members (including events). Let's see if this design is feasible. |
Beta Was this translation helpful? Give feedback.
-
But my current idea is that the implementing class can choose. It can choose whether or not to have these private fields declared from the interface. This means that the private fields declared by the interface are virtual. |
Beta Was this translation helpful? Give feedback.
-
To be more specific, if this interface is not instantiated by a class, then the private fields it declares do not exist. Only after a class that accepts all the private fields of this interface instantiates this interface do these private fields truly exist. But there is also a problem that needs to be restricted, that is, in order to reduce complexity, it should be stipulated that there are only two choices for the implementing class, and there is no intermediate situation, that is, there is no situation where only part of the fields are implemented. |
Beta Was this translation helpful? Give feedback.
-
See: #1212 |
Beta Was this translation helpful? Give feedback.
-
I believe there needs to be an additional restriction: the type of the virtual private fields of the interface must be limited to basic types and their corresponding arrays. More specifically, they must be numbers, dates, strings, or their corresponding arrays. Other collection types are not allowed, such as fields like List<int> should be excluded. This is to reduce complexity and make this rule specifically serve the implementation of interfaces, to avoid improper use, such as using calls from complex objects in the default implementation of the interface. At the same time, this design also retains the right to fall back and increases the constraint ability of the interface. In terms of the principle that the interface is a constraint, it has not been violated or destroyed. |
Beta Was this translation helpful? Give feedback.
-
It's too complicated, and probably it would be better to keep the interface lightweight and simple! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Suggestions for Enhancing C# Interfaces
We know that an interface is essentially a constraint, typically constraining public methods and properties. However, with the advancement of technology, could we enhance the default implementation capabilities of an interface by allowing it to declare private fields that don't really belong to itself but only constrain its implementing classes?
(Please forgive any inaccuracies or errors in my expression as my English proficiency is not very good and I've used machine translation. Also, I may not be able to respond promptly.)
Beta Was this translation helpful? Give feedback.
All reactions