In C #, what is the difference between equality members and the equalizer, and which should you use?

Please explain the difference between the C # equality members and the equalizer, and how to choose which one is required and in what circumstances are they used?

I am particularly interested in which ones are needed for LINQ operations or another language built into comparisons in .NET operations or types - for example, dictionary keys.

Thanks.

+5
source share
2 answers

Assumption - C # Equality Members - Example. Equality Method (...). C # Equality Comparer - e.g. IEqualityComparer interface.

In the short answer, with Equality Compare you can increase the usability of your code.

For example, you are building a vehicle catalog, and you want to make sure that the key, which is the model number and code, is not the same. Now this logic is for any vehicles. Thus, it is a good idea to define a default comparison and use it everywhere.

Here is an example of your help:

using System; using System.Collections.Generic; using ConsoleApplication3; public class Program { internal class Car : IVehicle { public List<string> Features { get; set; } public string ModelNumber { get; set; } public string ModelCode { get; set; } } internal class Bike : IVehicle { public string ModelNumber { get; set; } public List<string> Features { get; set; } public string ModelCode { get; set; } } public static void Main() { var carCatelogue = new Dictionary<Car, int>(new GlobalEqualityComparer()); var bikeCatelogue = new Dictionary<Bike, int>(new GlobalEqualityComparer()); carCatelogue.Add(new Car() { ModelCode = "100", ModelNumber = "CAR-01", Features = new List<string> { "BEST ENGINE", "5 GEAR", "SPOTY" } }, 5); carCatelogue.Add(new Car() { ModelCode = "100", ModelNumber = "CAR-02", Features = new List<string> { "SUPER FAST ENGINE", "4 GEAR", "SPOTY RED" } }, 10); // This Statement will throw exception because car-02 key already exists. carCatelogue.Add(new Car() { ModelCode = "100", ModelNumber = "CAR-02", Features = new List<string> { "SUPER FAST ENGINE", "4 GEAR", "SPOTY RED" } }, 10); bikeCatelogue.Add(new Bike() { ModelCode = "200", ModelNumber = "BIK-01", Features = new List<string> { "800 CC", "10 GEAR", "SPOTY BLACK" } }, 5); // this will throw exception because the key is aleady exists. bikeCatelogue.Add(new Bike() { ModelCode = "200", ModelNumber = "BIK-01", Features = new List<string> { "800 CC", "10 GEAR", "SPOTY BLACK" } }, 5); } private class GlobalEqualityComparer : IEqualityComparer<IVehicle> { public bool Equals(IVehicle x, IVehicle y) { return x.ModelNumber.Equals(y.ModelNumber, StringComparison.CurrentCultureIgnoreCase) && x.ModelCode.Equals(y.ModelCode, StringComparison.CurrentCultureIgnoreCase); } public int GetHashCode(IVehicle obj) { return string.Format("{0}{1}", obj.ModelCode, obj.ModelNumber).GetHashCode(); } } } 

In the case of using members like equals, you have to write the same logic for Car as well as for Bike.

Now that you would like to use where it will be, it will be completely reduced to you.

If you are happy with the Equal ie base, compare the link to the link or value to the value, then you can stay with it and you do not need to redefine it.

If you are specific to any issue, you need to give an example.

Hope this helps.

+4
source

Building on the help of Resharper, Equality members will create methods to compare your class X object with another class X object and an object of type object . Basically, compare everything. When comparing with an object, you check that it is not null and that it is an X object that returns false if either fails, and then make sure they are equal, depending on what properties of the class you decide to compare.

The equality comparator creates a class (object) that takes care of comparing two objects of type X

For example, having the string s1 and s2 , with elements of equality, you can do: s1.Equals(s2) and s1.Equals(some_other_object) , and using the comparator you can do: String.Equals(s1,s2) .

The same, but different.

+1
source

Source: https://habr.com/ru/post/1202919/


All Articles