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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:24:00+00:00 2026-06-01T01:24:00+00:00

Possible Duplicate: Can I overload an == operator on an Interface? I do understand

  • 0

Possible Duplicate:
Can I overload an == operator on an Interface?

I do understand how to overload operator== for a single class. I even made it work with inheritance. But I don’t seem to be able to implement it for an interface. Imagine the following:

IFoo foo1 = new Foo(42);
IFoo foo2 = new Foo(42);
Assert.IsTrue( foo1 == foo2 ); // fails

As you might have guessed I want classes implementing IFoo behave like a value type (at least when comparing for equality). As you can see the concrete type Foo of foo1 and foo2 is no longer “known” when the Assert happens. All that is known is their interface IFoo.

Of course I could use IEquatable<IFoo> and write foo1.Equals(foo2) instead, but that is not the point here. And if IFoo were an abstract base class instead of an interface I could overload operator== no problem. So how do I do the same for an interface?

The problem basically boils down to being unable to write the following, because at least one of left and right must be Foo:

public interface IFoo : IEquatable<IFoo>
{
  int Id { get; }
}

public class Foo : IFoo
{
  public int Id { get; private set; }
  ... // some implementation here

  public static bool operator== ( IFoo left, IFoo right ) // impossible
  {
    ... // some null checks here
    return left.Id == right.Id;
  }
}

Is overloading operator== for an interface impossible or is there a trick I missed?

  • 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-06-01T01:24:01+00:00Added an answer on June 1, 2026 at 1:24 am

    It isn’t possible to overload an operator on an interface. At their core overloaded operators are basically static methods which the compiler maps operators to. This means overloaded operators have all of the same issues that static methods in an interface do and hence are disallowed.

    The best way to specify that an interface has non-reference equality semantics is to have it derive from IEquatable<T>. This is by no means a binding commitment for the caller or implementer but it’s a hint to both.

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

Sidebar

Related Questions

Possible Duplicate: Java operator overload In c++, we can perform the operator overloading. But
Possible Duplicate: What’s the right way to overload operator== for a class hierarchy? In
Possible Duplicate: Operator Overloading with C# Extension Methods How can i overload those operators,
Possible Duplicate: «F(5)» and «int x; F(x)» to call different functions? Can we overload
Possible Duplicate: How do I overload the square-bracket operator in C#? Basically I want
Possible Duplicate: Operator overloading I was wonder how can I over load the Conditional
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've got
Possible Duplicate: Can’t operator == be applied to generic types in C#? I've coded
Possible Duplicate: Any reason to overload global new and delete? In c++ you can
Possible Duplicate: How do I check for nulls in an ‘==’ operator overload without

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.