I am trying to implement record types to make comparing less complex. But I ran into issues comparing records to each other. I expect records to be equal when properties are equal to each other but this isn't the case in my code..
I have a record class called MyAlarm with some properties. It inherits from the interface IAlarm which is empty.
public record MyAlarm : IAlarm
{
public int Prop1 { get; init; }
public int Prop2 { get; init; }
}
A class called VisibleAlarm has the IAlarm as property plus some extra data.
public record VisibleAlarm
{
public IAlarm InternalAlarm { get; init; }
public string Message { get; set; }
}
In a class called AlarmService the VisibleAlarms are listed and updated when needed. This service has a List of visible alarms.
Further in this alarm service the list is checked if an alarm already exists depending on the IAlarm property. I have tried two methods:
public class AlarmService
{
private List<VisibleAlarm> _alarms = new();
// ... Other code that handles alarm stuff
private VisibleAlarm GetExistingAlarm1(IAlarm alarm)
{
return _alarms.FirstOrDefault(a => a.InternalAlarm == alarm);
}
private VisibleAlarm GetExistingAlarm2(IAlarm alarm)
{
foreach (var existingAlarm in _alarms)
{
if (existingAlarm.InternalAlarm == alarm)
{
return existingAlarm;
}
}
}
}
When debugging the code I see that the record properties of the existingAlarm.InternalAlarm and the given alarm match but still the GetExistingAlarm methods return both false. Am i missing something or must I implement a IEqualityComparer because of interface IAlarm?
Cheers