© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
7 replies
peppy

❔ Default implementation of a `static abstract` base interface member in derived interface

I am trying to implement an idea I saw in MessagePack for C#:
public interface IMemoryPackable<T>
{
    static abstract void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref T? value)
        where TBufferWriter : IBufferWriter<byte>;
    static abstract void Deserialize(ref MemoryPackReader reader, scoped ref T? value);
}
public interface IMemoryPackable<T>
{
    static abstract void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref T? value)
        where TBufferWriter : IBufferWriter<byte>;
    static abstract void Deserialize(ref MemoryPackReader reader, scoped ref T? value);
}

where using
static abstract
static abstract
surfaces the methods on the type itself, which I find very useful.

I came up with this:
public interface IDispatchable<T>
{
    public static abstract bool Initialize();
    public static abstract bool Serialize(in ROS<T> src, in Span<byte> dest, out int bytesWritten);
    public static abstract bool Deserialize(in ROS<byte> src, in Span<T> dest, int srcLen);
}
public interface IDispatchable<T>
{
    public static abstract bool Initialize();
    public static abstract bool Serialize(in ROS<T> src, in Span<byte> dest, out int bytesWritten);
    public static abstract bool Deserialize(in ROS<byte> src, in Span<T> dest, int srcLen);
}

Now- if
T
T
were a struct, I can provide good default impls, so I declare a derived interface that constrains
T
T
:
public interface IBlockDispatchable<T> : IDispatchable<T> where T : struct { }
public interface IBlockDispatchable<T> : IDispatchable<T> where T : struct { }

Can this interface now declare a default implementation of the static abstract members of
IDispatchable<T>
IDispatchable<T>
such that implementing structs surface that default implementation?

I know the derived interface can declare an explicit impl of the base interface method, but then the explicit impl is not available through the struct's type as I expect
static abstract/virtual
static abstract/virtual
members to be.

If this is not the correct way to approach this, I'm open to all suggestions on how I can provide specialized behavior to structs in this particular instance. Thank you!
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ abstract class virtual methods vs default implementation interface
C#CC# / help
4y ago
Mocking derived class with abstract base class
C#CC# / help
4y ago
✅ How to call base implementation in abstract class?
C#CC# / help
3y ago
Using a default implementation on an interface
C#CC# / help
2y ago