5

I found interesting C# library. And there is a strange syntax in the code, which I don't understand. What does such syntax mean:

IRequestHandler<,>

? The same as IRequestHAndler<T1, T2> or something else?

RandomB
  • 3,367
  • 19
  • 30
  • 4
    This is open generic mapping / binding, which means you can fill any combination, instead of defining all combinations. Normally supported for something like Dependency injection containers. I use open generics frequently for my ninject container mapping – Mrinal Kamboj Jan 22 '19 at 16:51
  • 1
    I just tested to write an interface definition and a variable declaration with `ITest<,>`, with C# 7.3, both give me a compilation error. Could you give a bit more context ? @MrinalKamboj – Pac0 Jan 22 '19 at 16:53
  • 1
    This "means" an open (not-constructed) generic type (in this case, a generic type actually needing two type arguments that are not yet specified). You cannot instantiate this type obviously. What it does in your interesting library depends on how it is used. We can't tell without seeing the actual code. – René Vogt Jan 22 '19 at 16:53
  • 1
    Relevant: [What is the difference between a generic type and a generic type definition?](https://stackoverflow.com/questions/2564745/what-is-the-difference-between-a-generic-type-and-a-generic-type-definition) – John Wu Jan 22 '19 at 16:54
  • 2
    @JohnWu I don't see any information on the syntax without the type argument but with the brackets, in the linked Q&A. – Pac0 Jan 22 '19 at 16:55
  • 2
    @Pac0 You can only use this syntax as a parameter to `typeof()`: `Type genericDictionaryType = typeof(Dictionary<,>); ` – NineBerry Jan 22 '19 at 17:08
  • ah, yes! Thank you @NineBerry. Indeed this is how you can get the "unbound variant" of a generic type. – Pac0 Jan 22 '19 at 21:53

0 Answers0