Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Object.Equals Method
DefinitionDetermines whether two object instances are equal. In this articleOverloads
Equals(Object)Determines whether the specified object is equal to the current object.
Parametersobj Object The object to compare with the current object. ReturnsBoolean
ExamplesThe
following example shows a
The The
In The following example defines a
Some languages such as C# and Visual Basic support operator overloading. When a type overloads the equality operator, it must also override the Equals(Object) method to provide the same functionality. This is typically accomplished by writing the Equals(Object) method in terms of the overloaded equality operator, as in the following example.
Because RemarksThe type of comparison between the current instance and the
Because the Object class is the base class for all types in the .NET Framework, the Object.Equals(Object) method provides the default equality comparison for all other types. However, types often override the Equals method to implement value equality. For more information, see the Notes for Callers and Notes for Inheritors sections. Notes for the Windows RuntimeWhen you call the Equals(Object) method overload on a class in the Windows Runtime, it provides the default behavior for classes that don't override Equals(Object). This is part of the support that the .NET Framework provides for the Windows Runtime (see .NET Framework Support for Windows Store Apps and Windows Runtime). Classes in the Windows Runtime don't inherit Object, and currently don't implement an Equals(Object) method. However, they appear to have ToString, Equals(Object), and GetHashCode methods when you use them in your C# or Visual Basic code, and the .NET Framework provides the default behavior for these methods. Note Windows Runtime classes that are written in C# or Visual Basic can override the Equals(Object) method overload. Notes for CallersDerived classes frequently override the
Object.Equals(Object) method to implement value equality. In addition, types also frequently provide an additional strongly typed overload to the The following example provides an illustration. It instantiates three StringBuilder objects with identical strings, and then makes four calls to
In the first case, the strongly typed StringBuilder.Equals(StringBuilder) method overload, which tests for value equality, is called. Because the strings assigned to the two
StringBuilder objects are equal, the method returns You can compare the current object to another object for reference equality by calling the
ReferenceEquals method. In Visual Basic, you can also use the Notes for InheritorsWhen you define your own type, that type inherits the functionality defined by the
For a value type, you should always override Equals, because tests for equality that rely on reflection offer poor performance. You can also override the default implementation of Equals for
reference types to test for value equality instead of reference equality and to define the precise meaning of value equality. Such implementations of Equals return The following example shows how to override the Object.Equals(Object) method to test for value equality. It overrides the Equals method for the
In addition to overriding Equals, you can implement the IEquatable<T> interface to provide a strongly typed test for equality. The following
statements must be true for all implementations of the Equals(Object) method. In the list,
Implementations of
Equals must not throw exceptions; they should always return a value. For example, if Follow these guidelines when overriding Equals(Object):
Guidelines for Reference TypesThe following guidelines apply to overriding Equals(Object) for a reference type:
Guidelines for Value TypesThe following guidelines apply to overriding Equals(Object) for a value type:
See also
Applies toEquals(Object, Object)Determines whether the specified object instances are considered equal.
ParametersobjA Object The first object to compare. objB Object The second object to compare. ReturnsBoolean
ExamplesThe following example illustrates the Equals(Object, Object) method and compares it with the ReferenceEquals method.
RemarksThe static
Equals(Object, Object) method indicates whether two objects,
See also
Applies toWhich of these methods of class string is used to compare two string objects?Using String. equals() :In Java, string equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are same then it returns true.
Which of these method of class string is used to compare two string objects for their equality Isequal () Isequal () equals () equals ()?Java String equals() Method
The equals() method compares two strings, and returns true if the strings are equal, and false if not. Tip: Use the compareTo() method to compare two strings lexicographically.
Which is the method of class string that is used to compare two string objects for their equality Mcq?Explanation: equals() method of string class returns boolean value true if both the string are equal and false if they are unequal.
Which of these method of class string is used to compare two string objects for their equality immersive reader 1 point equals () equals () Isequal () Isequal ()?Explanation: equals() is method of class String, it is used to check equality of two String objects, if they are equal, true is retuned else false.
|