在非泛型声明上不允许使用约束
找到的语法只能在泛型声明中用于将约束应用于类型形参。 有关详细信息,请参阅泛型。
下面的示例生成 CS0080,因为 MyClass 不是泛型类,并且 Foo 不是泛型方法。
namespace MyNamespace
{
public class MyClass where MyClass : System.IDisposable // CS0080 //the following line shows an example of correct syntax
//public class MyClass<T> where T : System.IDisposable
{
public void Foo() where Foo : new() // CS0080
//the following line shows an example of correct syntax
//public void Foo<U>() where U : struct
{
}
}
public class Program
{
public static void Main()
{
}
}
}