C# 如果泛型类型约束还必须在c中实现接口,那么类类型约束实现了什么#

C# 如果泛型类型约束还必须在c中实现接口,那么类类型约束实现了什么#,c#,generics,C#,Generics,在编写泛型方法和函数时,我看到Where类型约束被写成 public static void MyMethod<T>(params T[] newVals) where T : IMyInterface publicstaticvoidmymethod(参数T[]newVals),其中T:IMyInterface 而且 public static void MyMethod<T>(params T[] newVals) where T : class, IMyInte

在编写泛型方法和函数时,我看到Where类型约束被写成

public static void MyMethod<T>(params T[] newVals) where T : IMyInterface
publicstaticvoidmymethod(参数T[]newVals),其中T:IMyInterface
而且

public static void MyMethod<T>(params T[] newVals) where T : class, IMyInterface
publicstaticvoidmymethod(params T[]newVals),其中T:class,IMyInterface
“类”类型约束增加了什么吗?我不认为结构可以实现接口,但我可能错了


谢谢

结构可以实现接口。那么这个

where T : class, IMyInterface
要求类型
T
既是
class
又是实现名为
IMyInterface
的接口的
class

例如,这是Int32结构的声明:

[SerializableAttribute]
[ComVisibleAttribute(true)]
public struct Int32 : IComparable, IFormattable, 
                      IConvertible, IComparable<int>, IEquatable<int>
[SerializableAttribute]
[ComVisibleAttribute(真实)]
public struct Int32:IComparable、IFormattable、,
i可转换、i可比较、i可计算

正如您所看到的。

A
struct
可以实现一个接口,因此要求泛型类型
T
既是一个
类,也是实现指定接口的双重约束是非常合理的

字典中考虑这一点:

[Serializable, StructLayout(LayoutKind.Sequential)]
public struct Enumerator : 
    IEnumerator<KeyValuePair<TKey, TValue>>, IDisposable, 
    IDictionaryEnumerator, IEnumerator
{
    //  use Reflector to see the code
}
[可序列化,结构布局(LayoutKind.Sequential)]
公共结构枚举器:
IEnumerator,IDisposable,
IDictionaryEnumerator,IEnumerator
{
//使用反射器查看代码
}

Yes结构可以实现接口。是的,结构可以实现接口。请参阅:
Enumerator
是实现接口的结构示例。谢谢-我本来会把你标记为答案的(两个答案都有我的假设错误的明显例子),但多默在1分钟内得到了答案earlier@Brent没问题,伙计。