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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:01:31+00:00 2026-05-31T03:01:31+00:00

I have this class (a partial listing): class CiscoSwitch { private string _SwitchName =

  • 0

I have this class (a partial listing):

class CiscoSwitch 
{
  private string _SwitchName = string.Empty;
  public SwitchName {get {return _SwitchName;} set{_SwitchName=value; }}
}

I have 2 lists of CiscoSwitch objects. I am trying to compare them to pick out the ones that are not duplicates. I only want the duplicates. I tried a Lambda expression but got a compiler error that CiscoSwitch was a non-delgate type.

I am now wondering about something like this – it would allow me to use the List.Except() method (I think):

static class SwitchComparer
{ 
  static bool CompareSwitchNames(CiscoSwitch s1, CiscoSwitch s2)
         {
            if (sw1.SwitchName == s2.SwitchName) {return true;}
             else {return false;}
         }
}

     // to find the differences 
 // this is a method of the CiscoSwitchClass
private List<CiscoSwitch> FindDifferences(List<CiscoSwitch> List1, List<CiscoSwitch> List2)
{
       return List1.Except(List2, SwitchComparer.CompareSwitchNames();
 }

this could also be done with a foreach but I think this way is a lot cleaner, if it is correct. I am also thinking there are other attributes of a CiscoSwitch I might want to compare some day so could add methods to the SwitchComparer class as I need them.

  • 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-31T03:01:34+00:00Added an answer on May 31, 2026 at 3:01 am

    No, just having a single method like that won’t help you. You need to implement an IEqualityComparer<CiscoSwitch> to pass to Enumerable.Except – and even then your code would need to be:

    return List1.Except(List2, new SwitchComparer()).ToList();
    

    Overriding Equals and GetHashCode within CiscoSwitch will do the trick more naturally though – and ideally you should implement IEquatable<CiscoSwitch> too.

    However, it’s worth noting that mutable types like this don’t play terribly nicely with things like Dictionary<,> – if you change an object in a way which affects its hash code after you’ve inserted it as a key into the dictionary, you won’t be able to get at it again. Consider making the type immutable if you can.

    A couple of other points to note:

    • Any time you write:

      if (condition)
      {
          return true;
      }
      else
      {
          return false;
      }
      

      you should instead write the far simpler:

      return condition;
      

      So your CompareSwitchNames method would be:

      static bool CompareSwitchNames(CiscoSwitch s1, CiscoSwitch s2)
      {
      return s1.SwitchName == s2.SwitchName;
      }

    • Your parameter names for FindDifferences should follow .NET naming conventions (e.g. list1 and list2)

    • Using Except will only find you the elements in the first list which aren’t in the second list; if you need to find the symmetric difference, consider using HashSet<T> explicitly.

    EDIT: If you wanted to have multiple ways of comparing, you could have something like:

    public static class SwitchComparers
    {
        public static readonly IEqualityComparer<CiscoSwitch> ByName =
            new ByNameComparer();
    
        public static readonly IEqualityComparer<CiscoSwitch> ByCost =
            new ByCostComparer();
    
    
        private sealed class ByNameComparer : IEqualityComparer<CiscoSwitch>
        {
            // Implementation
        }
    
        private sealed class ByCostComparer : IEqualityComparer<CiscoSwitch>
        {
            // Implementation
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class called Table: class Table { public string Name { get
I have this class. public class Foo { public Guid Id { get; set;
I have this class: public partial class Window1 : Window { public String Name2;
I have this class: public class UploadFile : INotifyPropertyChanged { private string name; public
I have this C# code: public partial class Continue : Form { public Continue(string
I have this code: public partial class FrmPrincipal : Form { private Image imagen;
I have a constructor question for C#. I have this class: public partial class
say I have this control: public partial class bloc999 : UserControl { bloc999Data mainBlock
I have this user defined function. public partial class UserDefinedFunctions { static int i;
I have a program that looks something like this: public partial class It {

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.