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 7494911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:50:55+00:00 2026-05-29T17:50:55+00:00

When working with the List class from System.Collections.Generic, methods like Contains or IndexOf will

  • 0

When working with the List class from System.Collections.Generic, methods like Contains or IndexOf will compare the passed reference’s object using either the Equals method implemented from IEquatable, or the overridden Equals method provided by the Object class. If Object.Equals is not overridden, it will check whether the passed reference points to the same object as itself.

My question is: Is there a way of making List compare by reference if Equals is overridden?
The code below will remove the item from the list:

class Program
{
    static void Main(string[] args)
    {    
        var s1 = new SomeClass() { A = 5 };
        var s2 = new SomeClass() { A = 5 };
        var list = new List<SomeClass>();
        list.Add(s1);
        list.Remove(s2); // s1 will get removed, even though s2 has been 
                         // passed, because s1's Equals method will return true.

    }
}

class SomeClass
{
    public int A { get; set; }

    public override bool Equals(object obj)
    {
        SomeClass s = obj as SomeClass;
        if (s == null)
        {
            return false;
        }
        else
        {
            return s.A == this.A;
        }
    }   
}

Let’s say I’m unable to remove SomeClass’ implementation of Equals, is there a way of making List compare by reference instead of value?

  • 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-05-29T17:51:00+00:00Added an answer on May 29, 2026 at 5:51 pm

    You can use List.RemoveAll and in your predicate, compare the items with Object.ReferenceEquals.

    list.RemoveAll(item => object.ReferenceEquals(item, s2));
    

    The code did successfully remove the 1 items when debugging from Visual Studio 2010 Express.

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

Sidebar

Related Questions

I have this code that is working fine for me: using System; using System.Collections.Generic;
I have a recursive function working on a list, the function contains a loop
I am working with a paginated list like in the NerdDinner sample. I am
I am working on a script that will scroll through list elements by clicking
I'm working on an application that uses a list to handle previous guesses from
I am working on a JTable that has one column that contains a List<List<String>>
I am working on one C# console application which create List collection of class
This is roughly the code I'm working with now: List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
please look at this example of drop down checkbox list im working on.. http://jsfiddle.net/Yojik/VJHVK/143/
I'm working on a To-do list for Mobile using phone gap. I use sqlite

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.