Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Generic parameter constructor parameter constraints #9968

Closed
ghost opened this issue Mar 22, 2016 · 2 comments
Closed

Proposal: Generic parameter constructor parameter constraints #9968

ghost opened this issue Mar 22, 2016 · 2 comments
Labels
Area-Language Design Feature Request Resolution-Duplicate The described behavior is tracked in another issue

Comments

@ghost
Copy link

ghost commented Mar 22, 2016

I propose to allow the definition of constructor parameter constraints for generics. Currently the new() constraint is permitted, but parameters cannot be specified.

Proposed syntax examples

public class Example<TThing> where TThing : ThingBase, new(string, bool)
{
    public void DoWork(string a, bool b)
    {
        TThing t = new TThing(a, b);

        t.Run();
    }
} 

A more practical example, which is useful when following a pattern of having a model filled by a data object.

public class Example2<TThing, TThingData> where TThing : new(TThingData)
{
    public TThing DoWork(int id)
    {
        var thingData = DataAccess.GetThingData(id);

        return new TThing(thingData);
    }
}
@vcsjones
Copy link
Member

I think this has been discussed before and determined that it would require support from the CLR before C# could support it. Currently CLR only supports new().

@alrz
Copy link
Member

alrz commented Mar 22, 2016

Dup of #2206

@gafter gafter added Resolution-Duplicate The described behavior is tracked in another issue Feature Request Area-Language Design labels Mar 22, 2016
@gafter gafter closed this as completed Mar 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design Feature Request Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

3 participants