4

I wonder, what rationale is behind lack of generic class type constraints for typed constructors? eg.

public class MyClass<T>
    where T : new(int)
{
    public T Create(int i)
    {
        return new T(i);
    }
}

Despite fact, that this may be quite easily (though IMO ugly) bypassed (by lambda-ctor), I can imagine no situation, when this constraint might cause any actual trouble or ambiguities.

Notice, that this is a language-structure question, not about a specific problem.

nicael
  • 18,550
  • 13
  • 57
  • 90
Spook
  • 25,318
  • 18
  • 90
  • 167
  • @ken2k There's no actual answer on *why* there are no such constraints. I'm not asking whether there are ones or how to overcome their lack. I'm asking, why C# is designed in such way, that there are none. – Spook Nov 12 '13 at 13:06
  • Next time you get on the bus, look behind you. Jon Skeet will be there with a post it note with the answer ... – Noctis Nov 12 '13 at 13:06
  • You many need to take a look at [this Eric Lippert's post](http://blogs.msdn.com/b/ericlippert/archive/2003/10/28/53298.aspx) – Sriram Sakthivel Nov 12 '13 at 13:06
  • @Spook It is explained there in comments below the accepted answer. – Ondrej Janacek Nov 12 '13 at 13:08
  • @SriramSakthivel I read that, but I doubt its the reason, otherwise C# wouldn't evolve in a rate it's currently evolving. I guess, that somewhere there is a sane explanation, but I cannot imagine one. – Spook Nov 12 '13 at 13:09
  • 1
    @Spook "I'm asking, why C# is designed in such way, that there are none" -> then who knows? Answers will be pure guesses. Most probably the feature is not useful enough to be implemented. But it's only a guess. – ken2k Nov 12 '13 at 13:10
  • @OndrejJanacek It is not. I know, that not every class deriving from class C will have ctor with specific parameters. But that's whole point of setting generic type contains, to ensure, that class derives from class C *and contains ctor with specific parameters*. That's no explanation at all (I left there my comment as well) – Spook Nov 12 '13 at 13:10
  • @ken2k 1. Ones, who designed the language; 2. Ones, who read the whole C# spec (I didn't, maybe there's a sentence about it); 3. Ones, who can imagine situation, when such construct would produce ambiguities or unsolvable problems. – Spook Nov 12 '13 at 13:12
  • Either eric or jon can only answer this question. They may come.. – Sriram Sakthivel Nov 12 '13 at 13:13

1 Answers1

1

I searched a little bit and found an answer. But since it is here on SO and I don't want to copy it, I will just post a link. It is an answer from Eric Lippert. I hope his answers means something to you.

https://stackoverflow.com/a/9741812/809009

It is somewhat long question there, but you can skip it and read only linked answer.

Community
  • 1
  • 1
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
  • 1
    Ok, that's the answer I searched for: *there is no standard way to describe "call the constructor that takes an int" in IL. We would have to either add a new concept to IL, or generate the code so that the generic constructor call used Reflection.* and that's fair enough for me. Thanks! – Spook Nov 12 '13 at 13:23