Re: Anonymous Method/Properties etc with <T>
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 18 Jan 2006 22:32:24 -0500
Rene,
The big reason is because of the way that generics are handled at run
time. The CLR is actually aware of them as generics. In C++ templates,
which is the base for your desired behavior, it can be verified at compile
time if the methods exist or not, and then act appropriately if they don't.
With Generics, that's not possible. The reason is that your
parameterized type might be used by another library/exe/whatever and insert
it's own type into the type parameter. You can't know this at compile time
as a result, since your type theoretically can be used anywhere else.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"Rene" <nospam@xxxxxxxxxx> wrote in message
news:eSCj0TKHGHA.3116@xxxxxxxxxxxxxxxxxxxxxxx
> We all know that we can't call custom methods or properties form generic
> type parameters (<T>) by default because the compiler will complain about
> his. For example, the following won't normally compile:
>
> T.MyCustomMethod()
>
> The compiler will tell me that it's not sure that the generic type
> parameters (<T>) will contain such method and will requires me to
> constrain the generic type parameters.
>
> My question is. Why doesn't the compiler allow for things such as
> anonymous methods or properties when using generic type parameters (<T>)?
> Why won't the compiler assume that such method with such return type is
> declared on <T> and then only complain when the type parameter *is*
> replace with the actual object it was meant to be replace with? Why the
> absolute necessity to constrain the type parameter?
>
> Thanks.
>
>
.
- Follow-Ups:
- Re: Anonymous Method/Properties etc with <T>
- From: Rene
- Re: Anonymous Method/Properties etc with <T>
- References:
- Anonymous Method/Properties etc with <T>
- From: Rene
- Anonymous Method/Properties etc with <T>
- Prev by Date: Visual Programming on Tablet PC
- Next by Date: Re: Sqlclient - connection pooling
- Previous by thread: Anonymous Method/Properties etc with <T>
- Next by thread: Re: Anonymous Method/Properties etc with <T>
- Index(es):
Relevant Pages
|