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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:13:32+00:00 2026-06-02T22:13:32+00:00

I know I can avoid boxing by adding my own Equals implementation. public struct

  • 0

I know I can avoid boxing by adding my own Equals implementation.

public struct TwoDoubles
{
    public double m_Re;
    public double m_Im;

    public TwoDoubles(double one, double two)
    {
        m_Re = one;
        m_Im = two;
    }

    public override bool Equals(object ob)
    {
           return Equals((TwoDoubles)ob);
    }
    public bool Equals(TwoDoubles ob)
    {
        TwoDoubles c = ob;
        return m_Re == c.m_Re && m_Im == c.m_Im;
    }
}

I can’t call this an override as much as an overload. By the magic of the runtime it does correctly call the correct Equals() implementation based on the type of the caller.

Why can’t I override and change the parameter type to TwoDoubles and let boxing occur by the power of the runtime on an as needed basis? Is it because C# doesn’t support parameter contravariance (if that’s the reason then why is it not supported…seems a small step from object o = new TwoDoubles())?

UPDATE
Clarification: object is a part of the inheritance hierarchy of a struct. Why can we not specify a more derived type as a parameter to override an implementation from a less derived type? This would allow us to write:

 public override bool Equals(TwoDoubles ob)
 {
        TwoDoubles c = ob;
        return m_Re == c.m_Re && m_Im == c.m_Im;    
 }

Which should be called when the variable is a TwoDouble even if said variable has been boxed into an object type.

  • 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-02T22:13:35+00:00Added an answer on June 2, 2026 at 10:13 pm

    Why can’t I override and change the parameter type to TwoDoubles?

    Because that would not be typesafe!

    class B
    {
      public virtual void M(Animal animal) { ... }
    }
    class D : B
    {
      public override void M(Giraffe animal) { ... }
    }
    
    B b = new D();
    b.M(new Tiger());
    

    And now you just passed a tiger to a method that actually only takes a giraffe!

    Same thing in your case. You’re overriding a method that takes any object with a method that can only take a struct; that’s not typesafe.

    Is it because C# doesn’t support parameter type contravariance?

    No, it is because you are asking for parameter type covariance, which is not typesafe.

    C# does not support parameter type contravariance either, but that’s not what you’re asking for.

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

Sidebar

Related Questions

With C# anonymous delegates, we can avoid declaration of extra method, I know in
I want to know can we turn on/off the GPS programmatically in iPhone?
This is something I know can be done somehow , because I've done it
I need to know can we add image in TextArea through StyleableTextField htmlText because
I have a query that I know can be done using a subselect, but
I have some following requirements, I don't know can we do this in iPhone
You can know if the event stack is empty calling the gtk.events_pending() method, but
I know I can insert an array into a Postgres database with pg_query .
I know I can do a page down using keyboard spacebar while browsing man
I know I can get the old value through UIInput#getValue() . But in many

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.