Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading