Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C#
C# Reference
C# Compiler Options
C# Compiler Errors
 Compiler Error CS0702
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual C# Reference: Errors and Warnings 
Compiler Error CS0702 

Error Message

Constraint cannot be special class 'identifier'

The following types may not be used as constraints: System.Array, System.Delegate, System.Enum, or System.ValueType.

Example

The following sample generates CS0702:

// CS0702.cs
class C<T> where T : System.Array  // CS0702
{
}
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
A workaround...      Mike Stall - MSFT   |   Edit   |   Show History

Here's a workaround. Instead of making the constraint on 'Delegate', you can make it on 'class' and then use a runtime cast to the generic type:

 public TDelegate GetDelegate<TDelegate>(IntPtr p) where TDelegate : class
{
Delegate function = Marshal.GetDelegateForFunctionPointer(p, typeof(TDelegate));
  
             // Ideally, we'd just make the constraint on TDelegate be
// System.Delegate, but compiler error CS0702 (constrained can't be System.Delegate)
// prevents that. So we make the constraint system.object and do the cast from object-->TDelegate.
object o = function;
             return (TDelegate)o;        
 }
  

It's true there's still a runtime cast in there, but the cast has to be somewhere and at least the signature has a type-safe return value.

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker