1

After updating packages to .net7 in roslyn code generation project, has error RS1024 Use 'SymbolEqualityComparer' when comparing symbols for IEqualityComparer implementation of GetHashCode()

public int GetHashCode([DisallowNull] TypeSymbolMetaType? obj) => System.HashCode.Combine(obj.TypeSymbol);

How to fix HashCode.Combine using SymbolEqualityComparer

2
  • 1
    Welcome to SO. What is TypeSymbolMetaType? Can you show more of your code? Commented Apr 26, 2023 at 15:09
  • 2
    What the error message suggests would be using public int GetHashCode([DisallowNull] TypeSymbolMetaType? obj) => SymbolEqualityComparer.Default.GetHashCode(obj.TypeSymbol);, as per docs with some rationale explained here. Commented Apr 26, 2023 at 15:16

1 Answer 1

0

Assuming TypeSymbol is ISymbol then you don't need to use Combine in this example:

public int GetHashCode([DisallowNull] TypeSymbolMetaType? obj) => SymbolEqualityComparer.Default.GetHashCode(obj.TypeSymbol);

In case you want to combine multiple properties the use the result of the SymbolEqualityComparer.Default.GetHashCode:

public int GetHashCode([DisallowNull] TypeSymbolMetaType? obj) => 
        System.HashCode.Combine(
            SymbolEqualityComparer.Default.GetHashCode(obj.TypeSymbol).
            ...);

P.S.

In some cases you might need to use SymbolEqualityComparer.IncludeNullability.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.