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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:30:10+00:00 2026-05-17T23:30:10+00:00

I was surprised recently to discover that the compiler is apparently not strict about

  • 0

I was surprised recently to discover that the compiler is apparently not strict about comparing interface references and am wondering why it works this way.

Consider this code:

class Program
{
    interface I1 {}
    interface I2 {}
    class C1 : I1 {}
    class C2 : I2 {}

    static void Main(string[] args)
    {
        C1 c1 = new C1();
        C2 c2 = new C2();

        I1 i1 = c1;
        I2 i2 = c2;

        bool x = c1 == c2;
        bool y = i1 == i2;
    }
}

The compiler says that I can’t compare c1 == c2, which follows. The types are totally unrelated. Yet, it does permit me to compare i1 == i2. I would expect it to error here with a compile-time failure but I was surprised to find out that you can compare any interface to any other and the compiler will never complain. I could compare, for example (I1)null == (IDisposable)null and no problem.

Are interfaces not objects? Are they a special type of reference? My expectation would be that a == would result in either a straight reference compare or a call into the concrete class’s virtual Equals.

What am I missing?

  • 1 1 Answer
  • 1 View
  • 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-17T23:30:10+00:00Added an answer on May 17, 2026 at 11:30 pm

    First off, note that Hans is quoting the correct section of the specification, but that the edition of the specification he is quoting has a typo which is relevant to your question. The corrected C# 4 specification says:

    The predefined reference type equality
    operators require one of the
    following:

    (1) Both operands are a value
    of a type known to be a reference-type
    or the literal null. Furthermore, an
    explicit reference conversion
    exists from the type of either operand
    to the type of the other operand.

    (2) One operand is a value of type T
    where T is a type-parameter and the
    other operand is the literal null.
    Furthermore T does not have the value
    type constraint.

    Unless one of these
    conditions are true, a binding-time
    error occurs.

    This explains your observation. There is an explicit reference conversion between any two interfaces because any two instances of two different interfaces could be referencing the same object. There could be a class C3 which implements both I1 and I2, and you could be doing a reference comparison of the same instance of C3, one converted to I1 and the other converted to I2.

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

Sidebar

Related Questions

I was recently surprised to know that this code compiles (at least on gcc
I've recently started learning Scala and was disappointed (but not surprised) that their generics
I recently came across this blog post which basically says that we should not
Recently I have started learning rails and was a little surprised that the default
Surprised that i havent been able to find this myself, but anyway. Let's say
I was surprised, if I noticed it right, that $this->Js->object() autosorts the array i
I'm a little surprised that it is not possible to set up a binding
I was recently surprised to note that compiling with /GS (Enable buffer security check)
I'm always surprised that even after using C# for all this time now, I
I saw a tweet recently that confused me (this was posted by an XNA

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.