
How Do You Overload == Operator in C# (C Sharp) ?Equals( ) is a virtual method belonging to Object. Its default behavior is to check for the values of the compared variables and return true if their values are same.
However C#
gives you the provision to override this method and overload the method
as well. In this article you will understand the default behavior of Equals(
) method and you will also come to know how to override this method. class sampleClass
{ Output of
this code will be: str1 is Hello Though Equals()
performs an operation, it is not an operator. It is a method. Hence you
can override the method and give a new meaning for it. You are still allowed
to overload the method with multiple definitions in your class. Here is
an example overriding Equals() method from the base class Object. It also
overloads the function to perform two different activities: class sampleClass
{ Output of
this code will be: instance3
is equal to instance1 In this example,
you override Equals() method in sampleClass and overload it to accept
System.Object as parameter in one method and accept sampleClass object
as parameter to the other overloaded method. You have
overridden and overloaded the methods to make them check for referential
equality instead of value equality. i.e. you check if the objects are
referencing to same instance instead of checking if both the objects hold
same value for its members.
_______________________________________________________________________
FREE
Subscription
Subscribe
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |