Introducing a new contextual keyword "sub" for interfaces #8037
Replies: 6 comments 2 replies
-
I don't understand, what defines this |
Beta Was this translation helpful? Give feedback.
-
/* public interface IA
{
int Source
{
get
{
//...
var x = sub.source + 10; // ok
return 2 * x; // ok
// var x2 = sub.source2 + 5; // Error: There is already a 'source' defined within this scope.
// return sub.source == 0; // Error: An expression involving 'sub' is inconsistent with the property's return type.
//...
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Further Explanation public interface IA
{
int Source
{
get => sub.source;
private set => sub.source = value;
}
}
public interface IB
{
int Source
{
get => sub.source2;
private set => sub.source2 = value;
}
}
// With the use of 'sub', there will be no conflict when multiple interfaces have properties with the same name during inheritance.
// According to existing C# rules, interface default implementation members can only be accessed through explicit interface member //implementation.
public class X : IA, IB
{
int source;
int source2;
}
// Furthermore, the introduction of 'sub' allows setting private setters, which adds meaningful constraints. Implementing classes could privately set these properties through the interface, maintaining encapsulation and consistency. |
Beta Was this translation helpful? Give feedback.
-
@nczsl You should edit your posts and format them with proper markdown code blocks. |
Beta Was this translation helpful? Give feedback.
-
I'd suggest that if there is to be a shorthand for an interface to declare state that needs to be provided by an implementing class that it be done so more explicitly. Having that state declared implicitly by virtue of the implementation details of a default method would be quite brittle, not to mention there's nothing about it that would explicitly declare the type of said state. There's no way for the compiler to know what the type of the expression I personally don't see what the issue is with the current mechanism for supporting this, namely that the interface declare a |
Beta Was this translation helpful? Give feedback.
-
thanks all. this topic need close, because it not my real need. so i publish a new discussions : #8040 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
/*
In C#, interfaces are used to constrain their implementing classes. Currently, there is no way to constrain non-public members of an implementing class. As of C# 12, interfaces now support default implementations where the default-implemented members can only be accessed via explicit interface invocation, which helps avoid confusion in cases of multiple interface inheritance. This has indeed introduced more convenience and ease with interface implementation and auto-implementation in C#. While I strongly support this feature, I propose a small improvement: introducing a contextual keyword similar to "value" in properties, called "sub". Contextual keywords lose their special meaning outside their context, thus preventing confusion and namespace pollution, making it a safer technical choice. Here's a usage example:
*/
Beta Was this translation helpful? Give feedback.
All reactions