Generic class derived from NameObjectCollectionBase and generic constraints

  • Thread starter Gabriel Lozano-Morán
  • Start date
G

Gabriel Lozano-Morán

I have a questions about the correct usage of deriving from classes that are
not generic and where to place the generic constraints:

public class MRUItems<T> : NameObjectCollectionBase where T : IMRUItem
{
public T this[int index]
{
get { return BaseGet(index) as T; }
set { BaseSet(index, value); }
}
}

Following compile error:
The type parameter 'T' cannot be used with the 'as' operator because it does
not have a class type constraint nor a 'class' constraint.

Note:
I don't want to use a generic sorted list, I just want to know how to
correctly derive from NameObjectCollectionBase and add the generic
constraint T : IMRUItem.

Thanks

Gabriel
 
M

Mattias Sjögren

Gabriel,
I don't want to use a generic sorted list, I just want to know how to
correctly derive from NameObjectCollectionBase and add the generic
constraint T : IMRUItem.

You've already done so. But you still can't use the as operator the
way you're trying since T may be a value type and the as operator only
can cast to reference types.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top