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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:33:59+00:00 2026-06-13T13:33:59+00:00

I want to define some generic class that class A : IClonable { clone();

  • 0

I want to define some generic class that

class A : IClonable
{
    clone();
}

I want to define a new class that derivative from class T but base class of T is A ==> so if the new class is B i will be able to call to Clone without define the IClonable in class B again.

How can i do this ?

  • 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-13T13:34:00+00:00Added an answer on June 13, 2026 at 1:34 pm

    I think you’re asking how to make the clone method available on MyClass when T inherits from ICloneable. That’s not possible without explicity stating that MyClass also inherits from IClonable since MyClass isn’t inheritting from T; it’s simply a class which has methods / properties which in some way relate to T (i.e. allowing operations to be formed on classes of type T.

    The closest I could come up with to allow you to access T through the generic class is to hack the default indexer property; that way by adding [1] to the end of your MyClass instance you’ll be looking at a single instance of a T clone.

        class A : ICloneable
        {
            public object Clone()
            {
                throw new NotImplementedException();
            }
            public override string ToString()
            {
                return "Demo";
            }
        }
        class B<T> where T : A
        {
            T myT;
    
            public B(T value)
            {
                this.myT = value;
            }
    
            //hack the default indexer to instead allow it to be used to return N clones of myT
            public IEnumerable<T> this[int index]
            {
                get
                {
                    for (int i = 0; i < index; i++)
                    {
                        yield return (T)this.myT.Clone();
                    }
                }
            }
        }
    
        class Program
        {
            public static void Main(string[] args)
            {
                B<A> myB = new B<A>(new A());
                Console.WriteLine( myB[1].ToString());
                Console.ReadKey();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to define a generic class in c++ that allow to execute my
I want to define some properties on a class using the [Indexable()] attribute in
I want to define some basic styles that I can combine. Let's say I
I'm trying to write a class that has a generic member variable but is
I have a class that does some generic code for Writing to a Database.
I have a global.asax in which I define some variables that I want to
I have derived a new control from Control base class and set the DefaultStyleKeyProperty
I have an abstract generic view-model that I use as a base-class for several
I use emacs in ubuntu 12.04. i want define some background or foreground in
I want to define some Contact Groups. I am wondering where and how does

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.