The comparison operator == and != have different semantics for different predefined types:
•Two expressions of type int are considered equal if they represent the same integer value.
•Two expressions of type object are considered equal if both refer to the same object, or if both are null.
•Two expressions of type string are considered equal if the string instances have identical lengths and identical characters in each character position, or if both are null.
Code:
string s = "George"; // Here is the assignment operator
string t = string.Copy(s); // Here also is the assignment operator
Console.WriteLine(s == t); //True
Console.WriteLine((object)s == (object)t); // false!!!
The last output is False because the predefined reference type equality operator is selected when one or both of the operands are of type object.
Reference type equality operators
********************************
The predefined reference type equality operators are:
Code:
bool operator ==(object x, object y);
bool operator !=(object x, object y);
The operators return the result of comparing the two references for equality or non-equality.
Since the predefined reference type equality operators accept operands of type object, they apply to all types that do not declare applicable operator == and operator != members. Conversely, any applicable user-defined equality operators effectively hide the predefined reference type equality operators.
The predefined reference type equality operators require the operands to be reference-type values or the value null. Furthermore, they require that a standard implicit conversion (§6.3.1) exists from the type of either operand to the type of the other operand. Unless both of these conditions are true, a compile-time error occurs.
Delegate equality operators
****************************
Every delegate type implicitly provides the following predefined comparison operators:
Code:
bool operator ==(System.Delegate x, System.Delegate y);
bool operator !=(System.Delegate x, System.Delegate y);
Two delegate instances are considered equal as follows:
•if either of the delegate instances is null, they are equal if and only if both are null.
•If either of the delegate instances has an invocation list (§15.1) containing one entry, they are equal if and only if the other also has an invocation list containing one entry, and either:
• both refer to the same static method, or
• both refer to the same non-static method on the same target object.
• If either of the delegate instances has an invocation list containing two or more entries, those instances are equal if and only if their invocation lists are the same length, and each entry in one’s invocation list is equal to the corresponding entry, in order, in the other’s invocation list.
Note that delegates of different types can be considered equal by the above definition, as long as they have the same return type and parameter types.
-obislavu-