我想了解为什么 C# 语言决定将此测试表达式作为错误。
interface IA { }
interface IB { }
class Foo : IA, IB { }
class Program
{
static void testFunction<T>(T obj) where T : IA, IB
{
IA obj2 = obj;
if (obj == obj2) //ERROR
{
}
}
static void Main(string[] args)
{
Foo myFoo = new Foo();
testFunction(myFoo);
Console.ReadLine();
}
}
在 testFunction 中,我可以创建一个名为 obj2 的对象并将其隐式设置为 obj 而无需强制转换。但是为什么我不能在不强制转换的情况下检查这两个对象是否相同?他们显然实现了相同的接口,那为什么会出错呢?