question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Harmonise `AvaloniaObject` equality comparison

See original GitHub issue

AvaloniaObject.Equals and GetHashCode are overridden, but only to seal the methods and prevent derived types from overriding them. This is fine at runtime, but causes static code analysis tools to think that the type has special equality logic. In my project this leads to annoying compile warnings every time we use == to compare two controls. Our CI rejects any commit containing these warnings.

To Reproduce Steps to reproduce the behavior:

  1. Compare two AvaloniaObject variables with ==
  2. Run Resharper on your solution with PossibleUnintendedReferenceComparison enabled
  3. Resharper will warn about the comparison

Expected behavior There are two options:

  1. AvaloniaObject should stop overriding Equals and GetHashCode. An analyzer diagnostic can be emitted to warn anyone who tries to override those methods in a derived type.
  2. AvaloniaObject should also provide operator == and operator !=, so that code analysis tools think that everything is customised and stops warning about it.

I think option 1 is preferable, because sealing the methods does not stop users from defining their own static equality operators.

Desktop

  • OS: N/A
  • Version 11 RC 1.1

Issue Analytics

  • State:closed
  • Created 4 months ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
maxkatz6commented, Jun 1, 2023

Note that WPF’s DependencyObject doesn’t override == operator. I suppose there is a perf penalty for doing so.

Is it really a problem with modern runtime? I am pretty sure if will be inlined to the same code nowadays.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Converting Binding Values
You can negate a binding value by prepending a "bang" operator: ! ... Convert( object? value, Type targetType, object? parameter, CultureInfo culture )...
Read more >
API - AvaloniaProperty.Equals(object) Method - Avalonia UI
AvaloniaProperty. Equals(object) Method. Namespace: Avalonia; Containing Type: AvaloniaProperty. Syntax. public override bool Equals(object obj). Parameters ...
Read more >
c# - WPF Event Binding to ViewModel (for non-Command ...
I've been able to find a ton of information on how to bind the Command of these objects to an ICommand in the...
Read more >
Equality Comparisons - C# Programming Guide
For records, value equality means that two variables of a record type are equal if the types match and all property and field...
Read more >
Commission Meeting - February 16,, 2023
(c) INSURANCE: Buyer agrees to insure the goods delivered under the Agreement in an amount at least equal to the purchase price against...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found