5

I know you can write:

class GenericClass<T> where T : new()
{ 

}

to enforce that T has an empty constructor.

My Qs are :

  1. can you enforce that T has a constructor with a specific type of parameter? Like:

    class SingletonFactoryWithEmptyConstructor<T> where T : new(int)
    
  2. can you enforce that T has a static function (let's say, void F()) so that you can use this function inside the generic class? Like :

    class GenericClass<T> where T : void F()
    { 
       void G ()
       {
           T.F();
       }
    }
    

    I know you can specify that T implements an interface but I don't want that. I want to specify that T has a static function.

nawfal
  • 70,104
  • 56
  • 326
  • 368
geth
  • 61
  • 2
  • possible duplicate of [How to constrain generic type to must have a construtor that takes certain parameters?](http://stackoverflow.com/questions/853703/how-to-constrain-generic-type-to-must-have-a-construtor-that-takes-certain-param), and also [calling-a-static-method-on-a-generic-type-parameter](http://stackoverflow.com/questions/196661/calling-a-static-method-on-a-generic-type-parameter) – nawfal Jul 16 '14 at 14:54

1 Answers1

6

No, there's nothing like this in C#.

I've previously suggested that "static interfaces" could express this reasonably neatly. They'd only be useful for generic type constraints (I suspect, anyway) but then you could express:

  • Constructors with arbitrary parameters
  • Static methods and properties
  • Operators

The last of these points is particularly interesting in my view, allowing things like a generic "Average" method over numeric types with suitable addition and division operators.

I believe some folks at MS have thought about something similar, but I haven't heard anything to suggest they're actively working on it.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    I shudder whenever I think of the kludges I've had to assemble to get around this limitation in the past.. (I deal a lot with generics). How I long for a static interface, nice solution to the problem. – Mania May 07 '11 at 08:07
  • 1
    Apparently this is a limitation of C# not of the CLR. F#, for example, allows to impose "explicit member constraints" like a presence of a method or property on a type... – Paul Michalik May 07 '11 at 11:23
  • @Paul: I'd be interested to see how that translates to IL... and does it include *static* members? – Jon Skeet May 07 '11 at 15:05
  • @Jon So'd be I... Haven't tried yet myself. Spin or cut and paste some code (e.g. from http://codebetter.com/matthewpodwysocki/2009/09/28/generically-constraining-f-part-ii/, he's citing you very frequently) and have a look at IL. And yes, static members are explicitely used in all the examples. I assume these features are not done by some sort of F# compiler hackery, it really seems to be supported by the CLI. Interestingly, explicit member constraints were marked as "not intended for common use" on MSDN until yesterday. Today, this restriction is gone. – Paul Michalik May 08 '11 at 12:02
  • @Paul, @Jon: I don't believe "static interfaces" (explicit member constraints, as they're known in F# - aka, duck typing) have anything to do with the CLR. It's purely a feature of the F# compiler, which is easily demonstrated by the fact that functions using such constraints must be marked `inline`. – Daniel Jun 03 '11 at 17:16
  • @Jon: Yes, F#'s explicit member constraints support static members (and operators). – Daniel Jun 03 '11 at 17:19
  • After a first (a bit more more than brief) reading on "Statically resolved type parameters (F#)" now I also believe that this is a feature of the F# compiler... Need to read up more on that though... – Paul Michalik Jun 04 '11 at 11:17
  • I have tried to capture the current state of play here: http://stackoverflow.com/a/18565782/746754 – acarlon Sep 02 '13 at 04:44