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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:56:12+00:00 2026-05-30T00:56:12+00:00

I don’t know if I’m missing something here but isn’t it strange that my

  • 0

I don’t know if I’m missing something here but isn’t it strange that my code below always raises an exception on List.Contains part although I know for sure that list contain that element:

using System;
using System.Linq;
using System.Collections.Generic;

class SomeClass
{
  public string param1 {get; private set;}
  public string param2 {get; private set;}

  private SomeClass(){}
  public SomeClass(string param1, string param2)
  {
    this.param1 = param1;
    this.param2 = param2;
  }
}

class SomeClass2
{
  private List<SomeClass> myList = new List<SomeClass>();

  public void Add(SomeClass someclass)
  {
    myList.Add(someclass);
  }

  public void Remove(SomeClass someClass)
  {
    // this part always rises an exception
    if(!myList.Contains(someClass))
      throw new System.ArgumentException("some error");
    else myList.Remove(someClass);
  }
}

class MainClass
{
  public static void Main (string[] args)
  {
    var _someClass = new SomeClass2();          
    _someClass.Add(new SomeClass("aaa", "bbb"));

    try
    {
      _someClass.Remove(new SomeClass("aaa", "bbb"));   
    }
    catch(Exception e)
    {
      Console.WriteLine(e.Message);
    }
  }
}
  • 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-30T00:56:16+00:00Added an answer on May 30, 2026 at 12:56 am

    Quote from the documentation of the Contains method:

    This method determines equality by using the default equality
    comparer, as defined by the object’s implementation of the
    IEquatable(Of T).Equals method for T (the type of values in the list).

    So you could implement IEquatable<T> on your objects if you want the Contains method to determine if 2 instances of SomeClass are equal:

    class SomeClass: IEquatable<SomeClass>
    {
        public string param1 { get; private set; }
        public string param2 { get; private set; }
    
        private SomeClass() { }
        public SomeClass(string param1, string param2)
        {
            this.param1 = param1;
            this.param2 = param2;
        }
    
        public bool Equals(SomeClass other)
        {
            return param1 == other.param1 && param2 == other.param2;
        }
    }
    

    Another possibility is to implement a custom EqualityComparer<T>:

    class SomeClassEqualityComparer : IEqualityComparer<SomeClass>
    {
        private static readonly SomeClassEqualityComparer _instance = new SomeClassEqualityComparer();
    
        public bool Equals(SomeClass x, SomeClass y)
        {
            return x.param1 == y.param1 && x.param2 == y.param2;
        }
    
        public int GetHashCode(SomeClass obj)
        {
            unchecked
            {
                int hash = 17;
                hash = hash * 23 + obj.param1.GetHashCode();
                hash = hash * 23 + obj.param2.GetHashCode();
                return hash;
            }
        }
    
        public static IEqualityComparer<SomeClass> Instance
        {
            get { return _instance; }
        }
    }
    

    and then use the following overload of the Contains method:

    if (!myList.Contains(someClass, SomeClassEqualityComparer.Instance))
        throw new System.ArgumentException("some error");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't let below code scare you away . The question is really simple, only
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query
Don't know if I worded the question right, but basically what I want to
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know how to explain it better but i'm trying to get a response
Don't know whats exactly going on, but it's definitely killing my time for nothing.
Don't know what's wrong here, when I run the application it says Specified method
Don't know if this is the right place to ask this, but I will
Don't know why, but sometimes LocationManager is still working also after closing application. I

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.