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

  • Home
  • SEARCH
  • 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 a collection of classes that inherit from an abstract class I created.
In Java, say you have a class that wraps an ArrayList (or any collection)
Suppose you have a collection of Foo classes: class Foo { public string Bar;
I have a class Agent with a property Id Given a collection of Agents
Using LINQ to SQL, I have an Order class with a collection of OrderDetails.
I have a custom class that implements ICollection , and this class is readonly,
I have a server that listens for a connection on a socket: public class
I've defined a method in a class: public void setCollection(Collection<MyClass>); and in another class
I have got a collection of viewModels(InputViewModel) in an other viewModel(ScenarioManager). each InputviewModel has
I have a class property exposing an internal IList<> through System.Collections.ObjectModel.ReadOnlyCollection<> How can 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.