Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we restrict the type be a delegate [duplicate]

Tags:

c#

.net

We can restrict the type be the class or struct. Can we strict the type be a delegate?

like image 723
user496949 Avatar asked Dec 21 '22 21:12

user496949


1 Answers

A Delegate is a class, and you would normally be able to specify a non-sealed class as a constraint. However, the language specification specifically excludes System.Delegate as a valid constraint in section 10.1.5.

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.
like image 148
Anthony Pegram Avatar answered Dec 24 '22 10:12

Anthony Pegram