Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8979405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:50:25+00:00 2026-06-15T19:50:25+00:00

I have a custom generic data structure that includes a Find method: public bool

  • 0

I have a custom generic data structure that includes a Find method:

public bool Find(TValue value, IEqualityComparer<TValue> comparer)
{
    foreach (var x in items)
    {
        if (comparer.Equals(value, x))
            return true;
    }
    return false;
}

I got a report recently from a client who said that it causes his equality comparer to throw NullReferenceException if value is null or if one of the items in the collection is null.

My initial response was that his IEqualityComparer<T>.Equals implementation was in error because it doesn’t deal gracefully with null values. But I haven’t been able to find any documentation to explicitly back me up. I have some evidence to indicate that I’m right, but nothing explicit.

First, it seems silly that I’d have change that simple call to comparer.Equals to:

if (x == null)
{
    if (value == null)
        return true;
}
else if (value != null && comparer.Equals(value, x))
    return true;

Second, the documentation for Object.Equals says, among other things:

  • x.Equals(null) returns false.
  • Implementations of Equals must not throw exceptions.

That, to me, is strong evidence that IEqualityComparer<T>.Equals should gracefully handle null parameters.

Other evidence is that the documentation for IComparer.Compare says:

Comparing null with any reference type is allowed and does not
generate an exception. A null reference is considered to be less than
any reference that is not null.

One would expect IEqualityComparer<T>.Equals to act similarly. It’s amusing to note, though, that the example given on that page will throw NullReferenceException if either parameter is null.

I’ve been through the documentation for Object.Equals, IEquatable<T>, IEqualityComparer<T>, and IEqualityComparer, and countless blog posts, articles, and SO questions. Nothing gives any specific guidelines about how to handle null parameters.

Do such guidelines exist? If not, what do the gurus recommend, and why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:50:26+00:00Added an answer on June 15, 2026 at 7:50 pm

    The closest method in the .NET framework itself after which all IEqualityComparer.Equals methods should be modeled is the static Object.Equals(object,object) method. According to the documentation, this method handles nulls gracefully. I think that this provides enough indication on the intent of the .NET designers: IEqualityComparer.Equals should handle nulls as well, and it should handle them in a similar way (i.e. treating two nulls as equal to each other).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom control with a data source of type IEnumerable (non-generic). Now
I have a generic list of custom objects and would like to reduce that
I have a generic-ed list of a custom type List<MyType> list = new List<MyType>();
I have custom validation rule: public function customRule($check) { } Inside this rule I
I have custom classes that I currently instantiate within App.xaml as resources. I want
I have custom event that has several different subscribers who will all use the
I have custom gallery. Gallery represents items that are frame layout. There are one
I have custom component that I can place in my layout file (XML) for
I have an ASP.NET web app that places several string data into an object
I have a custom s:GridItemRenderer that contains a single s:CheckBox and is used to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.