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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:37:01+00:00 2026-05-14T03:37:01+00:00

The use case is some what like this: public class SomeClass : ICloneable {

  • 0

The use case is some what like this:

public class SomeClass : ICloneable
{
    // Some Code

    // Implementing interface method
    public object Clone()
    {
        // Some Clonning Code
    }
}

Now my question is Why is it not possible to use “SomeClass(As it is derived from object)” as a return type of Clone() method if we consider the Funda’s of Covariance and Contravariance?

Can somebody explain me the reason behind this implementation of Microsoft ????

  • 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-14T03:37:01+00:00Added an answer on May 14, 2026 at 3:37 am

    A non-broken implementation of interface-implementation variance would have to be covariant in the return type and contravariant in the argument types.

    For example:

    public interface IFoo
    {
        object Flurp(Array array);
    }
    
    public class GoodFoo : IFoo
    {
        public int Flurp(Array array) { ... }
    }
    
    public class NiceFoo : IFoo
    {
        public object Flurp(IEnumerable enumerable) { ... }
    }
    

    Both are legal under the “new” rules, right? But what about this:

    public class QuestionableFoo : IFoo
    {
        public double Flurp(Array array) { ... }
        public object Flurp(IEnumerable enumerable) { ... }
    }
    

    Kind of hard to tell which implicit implementation is better here. The first one is an exact match for the argument type, but not the return type. The second is an exact match for the return type, but not the argument type. I’m leaning toward the first, because whoever uses the IFoo interface can only ever give it an Array, but it’s still not entirely clear.

    And this isn’t the worst, by far. What if we do this instead:

    public class EvilFoo : IFoo
    {
        public object Flurp(ICollection collection) { ... }
        public object Flurp(ICloneable cloneable) { ... }
    }
    

    Which one wins the prize? It’s a perfectly valid overload, but ICollection and ICloneable have nothing to do with each other and Array implements both of them. I can’t see an obvious solution here.

    It only gets worse if we start adding overloads to the interface itself:

    public interface ISuck
    {
        Stream Munge(ArrayList list);
        Stream Munge(Hashtable ht);
        string Munge(NameValueCollection nvc);
        object Munge(IEnumerable enumerable);
    }
    
    public class HateHateHate : ISuck
    {
        public FileStream Munge(ICollection collection);
        public NetworkStream Munge(IEnumerable enumerable);
        public MemoryStream Munge(Hashtable ht);
        public Stream Munge(ICloneable cloneable);
        public object Munge(object o);
        public Stream Munge(IDictionary dic);
    }
    

    Good luck trying to unravel this mystery without going insane.

    Of course, all of this is moot if you assert that interface implementations should only support return-type variance and not argument-type variance. But almost everyone would consider such a half-implementation to be completely broken and start spamming bug reports, so I don’t think that the C# team is going to do it.

    I don’t know if this is the official reason why it’s not supported in C# today, but it should serve as a good example of the kind of “write-only” code that it could lead to, and part of the C# team’s design philosophy is to try to prevent developers from writing awful code.

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

Sidebar

Ask A Question

Stats

  • Questions 366k
  • Answers 366k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Download the images into memory or a local cache using… May 14, 2026 at 4:30 pm
  • Editorial Team
    Editorial Team added an answer You could use netcat with the -u option to send… May 14, 2026 at 4:30 pm
  • Editorial Team
    Editorial Team added an answer If you want to share data between functions then create… May 14, 2026 at 4:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.