Show/Hide Toolbars

XSharp

The type parameter 'type parameter' cannot be used with the 'as' operator because it does not have a class type constraint nor a 'class' constraint

 

This error occurs if a generic type uses the as operator, but that generic type does not have a class type constraint. The as operator is only allowed with reference types, so the type parameter must be constrained to guarantee that it is not a value type. To avoid this error, use a class type constraint or a reference type constraint.

 

This is because the as operator could return null, which is not a possible value for a value type, and the type parameter must be treated as a value type unless it is a class type constraint or a reference type constraint.

Example

 

The following sample generates XS0413.

 

 

// XS0413.prg  
// compile with: /target:library