© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
7 replies
Honza K.

✅ public readonly bool Equals(...)? - SOLVED

I created struct for my needs of representing a phone number prefix and intellisense offered me a "potential fix" which after applying added readonly modifier to a method return type? Can you explain to me what this does as I see it first time in my 5 years of c#-ing?

public struct PhoneNumberPrefix : IEqualityComparer<PhoneNumberPrefix>
{   
    public PhoneNumberPrefix(int intValue, string stringValue)
    {
        IntValue = intValue;
        StringValue = stringValue;
    }
    public int IntValue { get; set; } = 0;
    public string StringValue { get; set; } = "";

    public readonly bool Equals(PhoneNumberPrefix x, PhoneNumberPrefix y)
    {
        return x.IntValue == y.IntValue && x.StringValue == y.StringValue;
    }

    public  int GetHashCode(PhoneNumberPrefix obj)
    {
        throw new NotImplementedException();
    }
}
public struct PhoneNumberPrefix : IEqualityComparer<PhoneNumberPrefix>
{   
    public PhoneNumberPrefix(int intValue, string stringValue)
    {
        IntValue = intValue;
        StringValue = stringValue;
    }
    public int IntValue { get; set; } = 0;
    public string StringValue { get; set; } = "";

    public readonly bool Equals(PhoneNumberPrefix x, PhoneNumberPrefix y)
    {
        return x.IntValue == y.IntValue && x.StringValue == y.StringValue;
    }

    public  int GetHashCode(PhoneNumberPrefix obj)
    {
        throw new NotImplementedException();
    }
}


And yes... It compiles 😄
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Marshalling bool
C#CC# / help
2y ago
✅ Readonly methods
C#CC# / help
2y ago
Understanding Equals() method
C#CC# / help
4mo ago
readonly struct and readonly struct instance members (optimization?)
C#CC# / help
4y ago