For user defined types your code will not compile.
In particular, he could not compile with the following error: "The operator" == "cannot be applied to operands of type" a "and" a ".
The == and! = Operators cannot work in a structure unless the structure explicitly overloads them.
. , Equals() ,
"... structs Object.Equals(Object) ( System.ValueType) . Equals stuct, , , , ."
( , OP):
== != , . Equals, == ! =, .
, , .
" ", .
class Program
{
static void Main()
{
MyMoreDerived a = new MyMoreDerived() { fbase = 1, fderived = 3 };
MyMoreDerived b = new MyMoreDerived() { fbase = 2, fderived = 3 };
if (a == b)
{
Console.WriteLine("MyDerived operator used: a == b");
}
a.fderived = 2;
b.fbase = 1;
if ((MyBase)a == (MyBase)b)
{
Console.WriteLine("MyBase operator used: a == b");
}
b.fderived = 2;
if ((object)a != (object)b)
{
Console.WriteLine("Default operator used: a != b");
}
}
class MyBase
{
public int fbase;
public static bool operator ==(MyBase x, MyBase y)
{
return x.fbase == y.fbase;
}
public static bool operator !=(MyBase x, MyBase y)
{
return x.fbase != y.fbase;
}
}
class MyDerived : MyBase
{
public int fderived;
public static bool operator ==(MyDerived x, MyDerived y)
{
return x.fderived == y.fderived;
}
public static bool operator !=(MyDerived x, MyDerived y)
{
return x.fderived != y.fderived;
}
}
class MyMoreDerived : MyDerived
{
}
}
, . , , "" .
.
MarshalByRefObject - , , /, . , . , , / , .
-, , imho , == !=.
( , .)