Equality operators - test if two objects are equal or not
Artikkeli
The == (equality) and != (inequality) operators check if their operands are equal or not. Value types are equal when their contents are equal. Reference types are equal when the two variables refer to the same storage.
You can use the is pattern matching operator as an alternative to an == test when you test against a constant value. The is operator uses the default equality semantics for all value and reference types.
Equality operator ==
The equality operator == returns true if its operands are equal, false otherwise.
int a = 1 + 2 + 3;
int b = 6;
Console.WriteLine(a == b); // output: Truechar c1 = 'a';
char c2 = 'A';
Console.WriteLine(c1 == c2); // output: False
Console.WriteLine(c1 == char.ToLower(c2)); // output: True
Huomautus
For the ==, <, >, <=, and >= operators, if any of the operands isn't a number (Double.NaN or Single.NaN), the result of operation is false. That means that the NaN value isn't greater than, less than, or equal to any other double (or float) value, including NaN. For more information and examples, see the Double.NaN or Single.NaN reference article.
Two operands of the same enum type are equal if the corresponding values of the underlying integral type are equal.
User-defined struct types don't support the == operator by default. To support the == operator, a user-defined struct must overload it.
C# tuples have built-in support for the == and != operators. For more information, see the Tuple equality section of the Tuple types article.
Reference types equality
By default, reference-type operands, excluding records, are equal if they refer to the same object:
C#
publicclassReferenceTypesEquality
{
publicclassMyClass
{
privateint id;
publicMyClass(int id) => this.id = id;
}
publicstaticvoidMain()
{
var a = new MyClass(1);
var b = new MyClass(1);
var c = a;
Console.WriteLine(a == b); // output: False
Console.WriteLine(a == c); // output: True
}
}
As the example shows, user-defined reference types support the == operator by default. However, a reference type can overload the == operator. If a reference type overloads the == operator, use the Object.ReferenceEquals method to check if two references of that type refer to the same object.
Record types equality
Record types support the == and != operators that by default provide value equality semantics. That is, two record operands are equal when both of them are null or corresponding values of all fields and automatically implemented properties are equal.
C#
publicclassRecordTypesEquality
{
publicrecordPoint(int X, int Y, string Name);
publicrecordTaggedNumber(int Number, List<string> Tags);
publicstaticvoidMain()
{
var p1 = new Point(2, 3, "A");
var p2 = new Point(1, 3, "B");
var p3 = new Point(2, 3, "A");
Console.WriteLine(p1 == p2); // output: False
Console.WriteLine(p1 == p3); // output: Truevar n1 = new TaggedNumber(2, new List<string>() { "A" });
var n2 = new TaggedNumber(2, new List<string>() { "A" });
Console.WriteLine(n1 == n2); // output: False
}
}
As the preceding example shows, for reference-type members their reference values are compared, not the referenced instances.
String equality
Two string operands are equal when both of them are null or both string instances are of the same length and have identical characters in each character position:
String equality comparisons are case-sensitive ordinal comparisons. For more information about string comparison, see How to compare strings in C#.
Delegate equality
Two delegate operands of the same run-time type are equal when both of them are null or their invocation lists are of the same length and have equal entries in each position:
C#
Action a = () => Console.WriteLine("a");
Action b = a + a;
Action c = a + a;
Console.WriteLine(object.ReferenceEquals(b, c)); // output: False
Console.WriteLine(b == c); // output: True
Tärkeä
Equal entries in an invocation list include all fixed parameters in the invocation, including the receiver. The receiver is the instance of an object represented by this when the entry is invoked.
C#
var o1 = newobject();
var o2 = newobject();
var d1 = o1.ToString;
var d2 = o2.ToString;
Console.WriteLine(object.ReferenceEquals(d1, d2)); // output: False
Console.WriteLine(d1 == d2); // output: False (different receivers)
Delegates that are produced from evaluation of semantically identical lambda expressions aren't equal, as the following example shows:
C#
Action a = () => Console.WriteLine("a");
Action b = () => Console.WriteLine("a");
Console.WriteLine(a == b); // output: False
Console.WriteLine(a + b == a + b); // output: True
Console.WriteLine(b + a == a + b); // output: False
Inequality operator !=
The inequality operator != returns true if its operands aren't equal, false otherwise. For the operands of the built-in types, the expression x != y produces the same result as the expression !(x == y). For more information about type equality, see the Equality operator section.
The following example demonstrates the usage of the != operator:
A user-defined type can overload the == and != operators. If a type overloads one of the two operators, it must also overload the other one.
A record type can't explicitly overload the == and != operators. If you need to change the behavior of the == and != operators for record type T, implement the IEquatable<T>.Equals method with the following signature:
Liity tapaamissarjaan ja luo skaalattavia tekoälyratkaisuja, jotka perustuvat reaalimaailman käyttötapauksiin muiden kehittäjien ja asiantuntijoiden kanssa.