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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:45:38+00:00 2026-05-16T11:45:38+00:00

Is there something like anonymous inner classes (used in Java) in C#? I explain

  • 0

Is there something like anonymous inner classes (used in Java) in C#?

I explain what I would use it for by example: I’m declaring and initializing field of type IDictionary<Person, Account> and I need to write custom IEqualityComparer<Person>. That is because I want two Persons to be treated as equal by the IDictionary when they have equal names and IDs (not only IDs as it is by default). I will not need this IEqualityComparer<Person> anywhere else in the code.

So I do I have to declare new class that implements IEqualityComparer<Person> to do this ? In Java I would use anonymous class, something like this(this is mixed C#-Java syntax, just to show what functionality I’m looking for):

IDictionry<Person, Account> myDict = new Dictionary<Person, Account>(
    new IEqualityComparer<Person>(){
        public bool Equals(Person a, Person b){
            return a.Id == b.Id && a.Name == b.Name;
        }

        public int GetHashCode(Person p){
            return p.Id.GetHashCode() * p.Name.GetHashCode();
        }
    });

Is something like this in C# ? I’m too lazy to write new class every time I need something like this.

Note: This is syntax question. I know how to write it, but I want to know if it’s possible to make the code shorter.

————————————————————————————————————————————————————————————————————————————————————————————————————

EDIT: How do you yourself code similar cases ? Do you create new class to implement the interface or what do you do ? Maybe you have some trick that I might like.

EDIT What about future support for anonymous classes like those in Java ? Have you heard something about it ?

EDIT: Well I see I’ll have to provide my actual code – not just an example. That’s because I don’t know if it’s going to work with Jon’s Skeet’s solution.

The actual reason why I don’t just implement Equals(object) and GetHashCode in the class itself is, that it’s class(entity) generated by E-R framework from model diagram. If I implemented it in class itself my code would be deleted from the class(entity) every time I update the model from database (using “update from database” feature). The class is actually called Font not Person. It has this properities:

Id: int
FamilyName:string
Size:int
Bold:bool
Italic:bool
Underlined:bool
Striked:bool
Foreground:Color

Where Color is another class (entity) generated from database.

This are properties of Color:

Id:int
Alpha:byte
Red:byte
Green:byte
Blue:byte

So I cannot modify Font, neither Color (if I don’t want to rewrite those changes over and over again every time I change database) What I want is to have this Dictionary:

private IDictionary<Font, Something> cache = new Dictionary<Font, Something>(new SomeEqualityComparer());

And the comparer SomeEqualityComparer should ensure that two Fonts would be considered equal if and only if all the properties listed above(except Id) are equal. In the case of last property Foreground two Colors are considered equal when all their properties(except Id) are equal.

Now if I use solution that Jon Skeet has kindly recommended me, I’m not sure if that can be ensured.
If I used something like:

private IDictionary<Font, Something> cache = new Dictionary<Font, Something>(ProjectionEqualityComparer<Font>.Create
(f => new { f.FontName, f.Size, f.Bold, f.Italic, f.Underlined, f.Striked, f.Foreground});

I’d guess that anonymous types call Equals(object) on all properties when their Equals(object) is called. However as I cannot override Color‘s Equals(object) it would not compare Colors as I want (using all properties except Id) so also the equality of Fonts would be tested incorrectly. Am I right ?

  • 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-16T11:45:39+00:00Added an answer on May 16, 2026 at 11:45 am

    In your last edit you mention that the reason that you don’t implement Equals and GetHashCode is because the code for your classes is auto-generated and you don’t want to have to re-implement that code each time you regenerate the code.

    That’s one of the scenarios for which partial classes were introduced in C#

    A lot of code generation tools will generate classes with the partial keyword to allow you to take advantage of that feature. Check if the classes that are being generated for your code are partial.

    In a separate file (or files) that won’t be overwritten when you regenerate the code, within the same assembly, you could have something like the following:

    partial class Font
    {
        public override bool Equals(object obj)
        {
            // ...
        }
    
        public override int GetHashCode()
        {
            // ...
        }
    }
    
    partial class Color
    {
        public override bool Equals(object obj)
        {
            // ...
        }
    
        public override int GetHashCode()
        {
            // ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Short version: In pure Java EE 6, is there something like Spring's Authentication Processing
I'm wondering if there is something like Hotswap/HotDelpoy/JRebel (known from Java World) in .NET
Is there something like a delegate in Haxe? This would come in handy when
In Java you can define a new class inline using anonymous inner classes. This
Is there something like serialize/unserialize PHP functions in jQuery? These functions return a string
Is there something like die in JavaScript? I've tried with break, but doesn't work
Is there something like the following in Apache Common Lang or Spring Utils or
Is there something like SESSION in Windows application? I want to store a few
is there something like API built atop the standard ProcessBuilder for calling system programs?
Is there something like VSTS2010 Team System Web Access (TSWA), if yes where is

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.