-3

I have an object like so:

public class MyObject {
    public string firstname { get; set; }
    public string lastname { get; set; }
    public int age { get; set; }
    public string occupation { get; set; }
}

I am trying to do a comparison of two objects but I want all strings to ignore case. Unfortunately the following won't compile:

// Does NOT allow me to call using ignore case
if (myObject1.Equals(myObject2, StringComparison.OrdinalIgnoreCase)) {
    Console.WriteLine("Match!");
}

Is there a way to accomplish this without manually checking each property in the object?

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
Jeremy P
  • 1,309
  • 2
  • 13
  • 22
  • 8
    Write your own `.Equals` override – Charlieface Jul 09 '21 at 17:41
  • 1
    Does this answer your question? [Comparing object properties in c#](https://stackoverflow.com/questions/506096/comparing-object-properties-in-c-sharp) and specifically [this](https://stackoverflow.com/a/506153/1797425) answer that involves overriding `.Equals` that @Charlieface mentions. – Trevor Jul 09 '21 at 17:44
  • 1
    The only way to get "free" value comparisons is with the the new [record type](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record) but even then you'd have to over-ride it to ignore case. – juharr Jul 09 '21 at 17:46

2 Answers2

0

You can override the Equals() method of your class (it is a method that every object has). Everything is well described in the documentation.

public override bool Equals(Object obj)
   {
      //Check for null and compare run-time types.
      if ((obj == null) || ! this.GetType().Equals(obj.GetType()))
      {
         return false;
      }
      else {
         Point p = (Point) obj;
         return (x == p.x) && (y == p.y);
      }
   }
Krzysztofz01
  • 432
  • 5
  • 13
0

To compare equality, you can implement Equals, let's do it with a help of IEquatable<MyObject> interface:

public class MyObject : IEquatable<MyObject> {
  public string firstname { get; set; }
  public string lastname { get; set; }
  public int age { get; set; }
  public string occupation { get; set; }

  public bool Equals(MyObject other) {
    if (ReferenceEquals(this, other))
      return true;
    if (null == other)
      return false;

    return 
      string.Equals(firstname, other.firstname, StringComparison.OrdinalIgnoreCase) &&
      string.Equals(lastname, other.lastname, StringComparison.OrdinalIgnoreCase) &&
      string.Equals(occupation, other.occupation, StringComparison.OrdinalIgnoreCase) &&
      age == other.age;
  }

  public override bool Equals(object obj) => obj is MyObject other && Equals(other);

  public override int GetHashCode() =>
    (firstname?.GetHashCode(StringComparison.CurrentCultureIgnoreCase) ?? 0) ^
    (lastname?.GetHashCode(StringComparison.CurrentCultureIgnoreCase) ?? 0) ^
    (occupation?.GetHashCode(StringComparison.CurrentCultureIgnoreCase) ?? 0) ^
     age;
}

then you can use the customized Equals

if (myObject1.Equals(myObject2)) {...}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215