I suggest you ...

Expand Generic Constraints for constructors

Currently when declaring a generic constraint on a Type parameter
ie.
public void DoSomething<T>() where T : new() { /* do something */ }

You can't specify that T has a constructor with specific parameters:
ie.
public void DoSomething<T>() where T : new(string, int) { /* do something */ }

473 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Michael PatersonMichael Paterson shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →
    AnonymousAnonymous shared a merged idea: Add new constraint on type parameters in C#: where new(parameters)  ·   · 

    11 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Giuseppe LippolisGiuseppe Lippolis commented  ·   ·  Flag as inappropriate

        another constraint interested to implement, it is a constraint that verifies if the generic type defined implements a specific generic Interface.
        example:

        public interface ISelectableItem <TValue>
        {
        bool IsSelected {get; sets;}
        TValue Value {get; sets;}
        }

        public class SelectableCollection <TItem>: ObeservableCollection <TItem>
        where TItem: ISelectableItem <?>
        {
        public void SelectAll()
        {
        foreach item in this
        item.IsSelected = true;
        }
        }

      • Marius GoppeltMarius Goppelt commented  ·   ·  Flag as inappropriate

        ... and please regard visibility. Currently, new constraint is always assumed as public. It suffices if the method which calls "Invoke" has sufficient access to the constructor. This is very useful for a factory pattern, where I don't want to expose the constructor.

        And don't forget VB.NET while you are at it! ;-)

      • CSharpJohnCSharpJohn commented  ·   ·  Flag as inappropriate

        @michael >> where T : int or long or float or double <<

        The issue is the lack of good generic math, specifically the lack of parent types or interfaces for int, long, float... like INumber, IInteger, IFloat.

        This said, I agree re; generic constraints
        ValueType does not cut it.

      • DavidDavid commented  ·   ·  Flag as inappropriate

        I like this. Here is yet another suggestion on the same line...
        as far as I'm aware, you can't do the following:

        public void DoSomething<T>() where T : Nullable
        OR
        public void DoSomething<T>() where T : NOT(Nullable)

        or somehow specify that T can (or cannot) be null. Recently I wanted to allow an "int?", or "long?" or "double?" (etc.) but not an "int", "long", or "double" but couldn't figure out how to do this. You CAN do this:

        public void DoSomething<T>() where T : class

        but that's not quite the same thing.

      • chhachha commented  ·   ·  Flag as inappropriate

        Yes, that's something I would also look forward to.

        Language: VB.NET

      • Ulrich BuUlrich Bu commented  ·   ·  Flag as inappropriate

        Yes. Another suggestion:

        public T Add(T a, T b) where T : int or long or float or double
        {
        return a + b;
        }

      Feedback and Knowledge Base