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

  • Home
  • SEARCH
  • 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 7908993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:11:37+00:00 2026-06-03T12:11:37+00:00

I created an interface with one method, the ability to copy the contents of

  • 0

I created an interface with one method, the ability to copy the contents of one object into another object of the same type (the actual functionality is irrelevant to the question).

public interface IDeepClonable
{
    void DeepClone<T>(T other);
}

I’m having trouble with a proper implementation.

What I really would like is to implement it like this (where this is inside ClassA, which implements IDeepClonable)

public void DeepClone<ClassA>(ClassA other)
{
    this.A = other.A;
}

However this does not work, as the ‘other’ object is not recognized as an instance of ClassA by the compiler (why?)

This does not work either, as it gives a ‘the constraints for type parameter T must match the (…) interface method.

public void DeepClone<T>(T other) where T : ClassA
{
    this.A= other.A;
}

I can solve all problems by changing the interface to take in an object instead of a generic constraint, but I was hoping for a more elegant solution.

I can also solve this by turning the interface into a generic interface, but then then that forces me to cast to that generic interface.

  • 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-03T12:11:38+00:00Added an answer on June 3, 2026 at 12:11 pm

    You’re trying to use the CRTP.

    You need to write

    public interface IDeepClonable<out T> where T : IDeepClonable<T>
    {
        void DeepClone(T other);
    }
    
    public class ClassA : IDeepClonable<ClassA> {
        void DeepClone(ClassA other) { ... }
    }
    

    However, this means that any code that uses IDeepClonable must itself become generic, which will end up becoming unwieldy.

    The CLR type system is not rich enough to do what you really want.

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

Sidebar

Related Questions

Lets go from the start Created New Xcode project,opened interface builder dragged one NSButton
I've created a class to hold an interface component on one of my web
I have created a .NET DLL which makes some methods COM visible. One method
I have 2 classes that have the exact same logic/workflow, except in one method.
I'm really frustrated with this one. I'm trying to extend a C# created interface
I've created two classes with methods with same name. In one of them it
I have created an interface with ServiceContract on interface and OperationContract on all its
I have a Tab Bar Controller created in Interface Builder Within the Tab Bar
I have a ViewController and UIWebView created in Interface Builder in it. I fill
I'm using a UITableView to display custom cells created with Interface Builder. The table

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.