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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:53:19+00:00 2026-05-12T05:53:19+00:00

I have a collection class with an Equals method that I want to pass

  • 0

I have a collection class with an Equals method that I want to pass in a method to do equality checking between each item. Furthermore, I want to allow the delegate type to operate on superclasses of T as well as T itself:

public delegate bool EqualityComparer<T>(T x, T y);

public class Collection<T>
{
    //...

    public bool Equals<U>(Collection<T> other, EqualityComparer<U> eq) where T : U
    {
        // code using eq delegate to test equality between
        // members of this and other collection
    }
}

Unfortunately, the compiler borks over this (‘Collection.Equals()’ does not define type parameter ‘T’). Is there any way of specifying this type of constraint/operation?

  • 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-12T05:53:20+00:00Added an answer on May 12, 2026 at 5:53 am

    No, I’m afraid you can’t specify a constraint like that. (I’ve wanted it too on occasion.)

    You could write a static generic method with two type parameters in a non-generic class though:

    public delegate bool EqualityComparer<T>(T x, T y);
    
    public class Collection
    {
        public static Equals<T, U>(Collection<T> first,
                                   Collection<T> second,
                                   EqualityComparer<U> comparer) where T : U
        {
    
        }
    }
    

    and you could even make that call an instance method on the generic class if you like:

    // Implementing the static method:
    return first.Equals(second, new EqualityComparer<T>(comparer));
    

    where the collection’s instance method would just be:

    public bool Equals(Collection<T> other, EqualityComparer<T> eq)
    {
        // ...
    }
    

    This uses the contravariance available for creating delegates from C# 2 onwards.

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

Sidebar

Related Questions

I have created a custom collection class that I am trying to bind to
I have an object that has a child collection: class Person { public string
I have a collection class like this: public class SomeDataCollection : List<ISomeData> { //
I have a collection class MyCollection<T> . I have implemented T this[string name] ,
I have Class and Student objects. Both have collection of another as property. Which
I have two classes and mapping for the collection: class User { Guid ID;
So say I have a collection of Bloops Class Bloop Public FirstName Public LastName
I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private
I have a collection similar to: Public Class MyCollection Inherits ObservableCollection(Of MyCollection) Private _Name
If I have a collection of objects: public class Party { LinkedList<Guy> partyList =

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.