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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:38:17+00:00 2026-05-21T21:38:17+00:00

Either I am missing something or .NET doesn’t support what Java does. I’d like

  • 0

Either I am missing something or .NET doesn’t support what Java does. I’d like to be able to avoid creating a small class just for the sake of implementing a small interface. For example, LINQ’s Except method expects IEqualityComparer. So I had to write a small class that implements the interface. However in Java I can simply do a new IEqualityComparer() { //interface declarations } and be done with it. So what’s the problem?

This is somewhat related to this post:

Can a C# anonymous class implement an interface?.

ADDITION:
At the moment, I added overrides for Equals and GetHashCode.

  • 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-21T21:38:17+00:00Added an answer on May 21, 2026 at 9:38 pm

    You’re correct, C# unlike Java, does not support the notion of anonymous inner classes which can implement interfaces. I’ve run into the exact same problem with IEqualityComparer and eventually came to the following solution.

    public static class Utils {
      private sealed class EqualityComparer<T> : IEqualityComparer<T> {
        private readonly Func<T, T, bool> m_equals;
        private readonly Func<T, int> m_getHashCode;
        internal EqualityComparer(
          Func<T, T, bool> equals,
          Func<T, int> getHashCode) {
          m_equals = equals;
          m_getHashCode = getHashCode;
        }
        public bool Equals(T left, T right) {
          return m_equals(left, right);
        }
        public int GetHashCode(T value) {
          return m_getHasheCode(value);
        }
      }
    
      public static IEqualityComparer<T> CreateEqualityComparer<T>(
        Func<T, T, bool> equals, 
        Func<T, int> getHashCode) {
        return new EqualityComparer<T>(equals, getHashCode);
      }
    }
    

    Now in places where I want an on the fly IEqualityComparer<T> i can just do the following

    var comparer = Utils.CreateEqualityComparer<Student>(
      (left, right) => left.Name == right.Name,
      value => value.Name.GetHashCode());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read this PHP RegEx page , but either I'm missing something, misreading something,
I am likely missing something obvious, but humor me... I always like to name
I'm creating a class which will contain a list of IP addresses, as Net::IP
Either for comparisons or initialization of a new variable, does it make a difference
I'm a bit confused from what I've heard Java doesn't do events. But I
Possible Duplicate: Does anyone beside me just NOT get ASP.NET MVC? I dont know
I have a feeling this is something very obvious that I'm missing, but here
I might be missing something here, I'm not sure. A Google search didn't really
Either I had a bad dream recently or I am just too stupid to
In either a Windows or Mac OS X terminal if you type... nslookup -type=SRV

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.