© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
12 replies
2spooky2play

✅ Pass in arguments to a generic constructor

    public T AddComponent<T>() where T: Component, new() {
        Component component = new T(this);
        return (T)AddComponent(component);
    }
    public T AddComponent<T>() where T: Component, new() {
        Component component = new T(this);
        return (T)AddComponent(component);
    }

i want to be able to create new instances of
T
T
in the method, but i get the error
'T': cannot provide arguments when creating an instance of a variable type
'T': cannot provide arguments when creating an instance of a variable type
. how can i make this work? one precondition is that all child of
Component
Component
will only have one argument in their constructor
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

✅ Using Activator.CreateInstance() when the constructor has arguments - not finding constructor
C#CC# / help
4y ago
How to pass custom arguments to MAUI button click
C#CC# / help
4y ago
❔ how to pass multiple arguments in apicall from ajex get url?
C#CC# / help
4y ago
Thread.sleep in a constructor
C#CC# / help
2y ago