Unable to access self-implemented "abstract" interface members. #7992
-
Why i cannot access self-implemented members in "abstract" interfaces. Example: public interface IBase64Content
{
public string ContentAsBase64
{
get => (this as XElement).Value.FromBase64();
set => (this as XElement).Value = value.ToBase64();
}
} When implementing that interface: public class Response : XElement, IXmppFactoryInfo, IBase64Content
{
static IEnumerable<XName> IXmppFactoryInfo.GetNames()
{
yield return SaslNames.Response;
}
public Response() : base(SaslNames.Response)
{
}
} This will not compile:
Instead i MUST do a explicit cast to interface type to use property |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
It is intended to be used like that. |
Beta Was this translation helpful? Give feedback.
-
Interface members aren't inherited like class members are. The members only exist through the interface, unless the type also declares the members, which is the typical case. But with default or explicit implementation, the interface member effectively does not exist on the type and cannot be called directly without calling through the interface member. There was a proposal to add a |
Beta Was this translation helpful? Give feedback.
It is intended to be used like that.
DIM does not exists on the implementing
class
, only on theinterface
.https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods#concrete-methods-in-interfaces