What exactly is a “Special Class”?

2019-01-30 16:49发布

After failing to get something like the following to compile:

public class Gen<T> where T : System.Array
{
}

with the error

A constraint cannot be special class `System.Array'

I started wondering, what exactly is a "special class"?

People often seem to get the same kind of error when they specify System.Enum in a generic constraint. I got the same results with System.Object, System.Delegate, System.MulticastDelegate and System.ValueType too.

Are there more of them? I cannot find any info on "special classes" in C#.

Also, what is so special about those classes that we can't use them as a generic type constraint?

7条回答
老娘就宠你
2楼-- · 2019-01-30 17:44

According to MSDN it's a static list of classes:

Compiler Error CS0702

Constraint cannot be special class 'identifier' The following types may not be used as constraints:

  • System.Object
  • System.Array
  • System.Delegate
  • System.Enum
  • System.ValueType.
查看更多
登录 后发表回答