© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
15 replies
Althos

❔ Is there a better way to implement this "pattern"?

Hi, I'm trying to implement a pattern where I have a non-generic interface that exposes certain functions and can be used by any other class without the need for a generic, and then a generic version of the interface that helps restrict types properly when creating concrete implementations, here is an example
 internal interface IVisualElement
    {
        IEnumerable<IVisualElement> Children { get; }
        void Draw(IVisualElement parent);
    }

    internal interface IVisualElement<in T> : IVisualElement where T : IVisualElement
    {
        void DrawGeneric(T parent);
    }

    internal class ConcreteVisualElement : IVisualElement<Node>
    {
        public IEnumerable<IVisualElement> Children { get; } = new List<IVisualElement>();

        public void DrawGeneric(Node parent)
        {
            throw new System.NotImplementedException();
        }

        public void Draw(IVisualElement parent)
        {
            DrawGeneric(parent as Node);
        }
    }
 internal interface IVisualElement
    {
        IEnumerable<IVisualElement> Children { get; }
        void Draw(IVisualElement parent);
    }

    internal interface IVisualElement<in T> : IVisualElement where T : IVisualElement
    {
        void DrawGeneric(T parent);
    }

    internal class ConcreteVisualElement : IVisualElement<Node>
    {
        public IEnumerable<IVisualElement> Children { get; } = new List<IVisualElement>();

        public void DrawGeneric(Node parent)
        {
            throw new System.NotImplementedException();
        }

        public void Draw(IVisualElement parent)
        {
            DrawGeneric(parent as Node);
        }
    }


Is there a name for what I'm trying to do, and is there a better way to implement it? Thanks a lot for your help!
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

Is there a better way to do this
C#CC# / help
3y ago
Is there a better way to write this QueryBuilder?
C#CC# / help
16mo ago
is there is a way to make this faster
C#CC# / help
3y ago