Generic class derived from NameObjectCollectionBase and generic constraints

  • Thread starter Thread starter Gabriel Lozano-Morán
  • Start date 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
 
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
 
Back
Top