Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# type parameters specification

Some special CLI types from mscorlib library (ArgIterator, TypedReference and RuntimeArgumentHandle types) cannot be used as generic type parameters to construct the generic types / methods:

void Foo<T>() { }
void Bar() { Foo<ArgIterator>(); }

provides the compiler error:

error CS0306: The type 'System.ArgIterator' may not be used as a type argument

But this is not documented at all in the C# specification.

Is this types are a part of CLI specification or this types provided by CLR implementation and the behavior described above should not be documented at C# spec?

like image 501
controlflow Avatar asked Jan 14 '10 13:01

controlflow


3 Answers

First off, Jon is again correct -- these guys are very special types whose values are not convertible to object, and so cannot be used as type arguments. All type arguments must be types whose values are convertible to object.

To answer your question about documentation:

None of the special features for handling variadic methods are documented. They are not a part of the C# language itself -- a conforming implementation of the language is not required to be able to do interop with languages that support variadic methods. Nor are these features documented in MSDN as part of the compiler documentation. These are not "officially supported" features.

This is unfortunate, but there's only so much budget available, and I think most people would agree that we'd do better to write features and fix bugs than to spend money documenting features that literally 99.99% of our users will never, ever use even if they were supported, which they're not.

If you want to go do interop in C# with variadic methods, you're on your own. Good luck!

like image 99
Eric Lippert Avatar answered Oct 18 '22 21:10

Eric Lippert


I believe it's because these types are "special" in that they can't be converted to object; only types which can be converted to object can be specified as type arguments. The same is true for pointers, by the way.

I can't find where this is documented (it's documented for pointers in 4.4.1) but Eric Lippert mentioned it in a comment the other day.

Is this just a matter of interest, or are you trying to actually do something using this kind of thing?

like image 36
Jon Skeet Avatar answered Oct 18 '22 23:10

Jon Skeet


All three of the examples that you provided are structs, and not classes, so I suspect that's the key to the problem. An example provided in the documentation on the compiler error message indicates also that if you use a pointer to a type in the generic it would fail.

  • ArgIterator
  • RuntimeArgumentHandle
  • TypedReference
like image 27
scwagner Avatar answered Oct 18 '22 22:10

scwagner