Search code examples
c#.net-6.0

Implement Interface through class member


Is it possible in C# to implement an Interface through a class member without explicitly returning the members implementation? I want something like this

interface IAttachement
{
    byte[] Data { get; }
    string Name { get; }
    long Size { get; }
}

class Attachement : IAttachement
{
    public byte[] Data { get; set; }
    public string Name { get; set; }
    public long Size { get; set; }
}

class Request : IAttachement
{
    public Attachement Attachement { get; set; } : IAttachement
}

Instead of

class Request : IAttachement
{
    public Attachement Attachement { get; set; }
    public byte[] Data => Attachement.Data;
    public string Name => Attachement.Name;
    public long Size => Attachement.Size;
}

Solution

  • No, this isn't a feature that C# offers.

    You can delegate interface implementation to a field/property/member, but you have to do it explicitly, as in your example.

    If you would like this feature adding to the language, you can raise an issue here.