C# Keywords Tutorial Part 28: equals

C# Keywords Tutorial Part 28: equals

C# is a programming language that is widely used by developers, as it comes with various tools and features that aid them in creating efficient and effective code. Among these features is the “equals” keyword, which is utilized to compare two objects for equality. In this blog post, we’ll delve into the specifics of the “equals” keyword in C#, and we’ll provide code examples to assist you in comprehending its effective use.

What is the “equals” Keyword in C#?

The “equals” keyword is used to compare two objects for equality. It is part of the System.Object class and is inherited by all other classes in C#. When you use the “equals” keyword to compare two objects, it will return a Boolean value (true or false) indicating whether the objects are equal.

The default implementation of the “equals” method in C# compares objects by reference. This means that if two objects have the same memory address, they are considered equal. However, if you want to compare two objects based on their properties, you will need to override the “equals” method.

Overriding the “equals” Method

To override the “equals” method, you will need to create a new method with the same name and signature as the default “equals” method. The signature of the “equals” method is:

public virtual bool Equals(object obj)

This method takes an object as a parameter and returns a Boolean value indicating whether the object is equal to the current instance.

Here is an example of how to override the “equals” method for a custom class in C#:

public class Person
{
? ? public string FirstName { get; set; }
? ? public string LastName { get; set; }
? ??
? ? public override bool Equals(object obj)
? ? {
? ? ? ? if (obj == null || !(obj is Person))
? ? ? ? ? ? return false;
? ? ? ??
? ? ? ? var otherPerson = (Person)obj;
? ? ? ??
? ? ? ? return this.FirstName == otherPerson.FirstName?
? ? ? ? ? ? ? ?&& this.LastName == otherPerson.LastName;
? ? }
}        


In this example, we have created a custom class called “Person” with two properties: FirstName and LastName. We have also overridden the “equals” method to compare two Person objects based on their FirstName and LastName properties.

In the “equals” method, we first check if the object is null or not an instance of the Person class. If either of these conditions is true, we return false. Otherwise, we cast the object to a Person object and compare the FirstName and LastName properties of the current instance and the otherPerson object.

Using the “equals” Keyword

Once you have overridden the “equals” method, you can use the “equals” keyword to compare two objects for equality. Here is an example:

var person1 = new Person { FirstName = "John", LastName = "Doe" };
var person2 = new Person { FirstName = "John", LastName = "Doe" };


bool areEqual = person1.Equals(person2);        

In this example, we have created two Person objects with the same FirstName and LastName properties. We then use the “equals” keyword to compare the two objects and assign the result to the “areEqual” variable. Since we have overridden the “equals” method to compare two Person objects based on their FirstName and LastName properties, the “areEqual” variable will be true.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了