Bruce Ge

  Home  |   Contact  |   Syndication    |   Login
  6 Posts | 0 Stories | 11 Comments | 0 Trackbacks

News

Archives

Post Categories

Although delegate type and Enum type are class types, we can not inherit from them as they are sealed classes after compile,
public delegate void DelegateTestType();
 
//Compile time error
class myTest : DelegateTestType
{
     ...
}
 
we can not make it as a type constraint either on class or method:
//Compile time error
class myTest<T> where T : DelegateTestType
{
      
}
 
From the C# 2.0 specification we can read (20.7, Constraints):
 
A class-type constraint must satisfy the following rules:
·         The type must be a class type.
·         The type must not be sealed.
·         The type must not be one of the following types: System.Array, System.Delegate, System.Enum, or System.ValueType.
·         The type must not be object. Because all types derive from object, such a constraint would have no effect if it were permitted.
·         At most one constraint for a given type parameter can be a class type.
In c# world we have to use some indirect ways, like type casting, wrapper class or examine the type is Delegate type, it is still a problem in C# 4.0, this is a bit sad as C++ can do it more than happily!
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Saturday, November 21, 2009 10:17 PM