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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:29:55+00:00 2026-05-15T00:29:55+00:00

After a bit of programming one of my classes used generics in a way

  • 0

After a bit of programming one of my classes used generics in a way I never seen before. I would like some opinions of this, if it’s bad coding or not.

abstract class Base<T> : where T : Base<T>
{
    // omitted methods and properties.
    virtual void CopyTo(T instance) { /*code*/ }
}

class Derived : Base<Derived>
{
    override void CopyTo(Derived instance)
    { 
         base.CopyTo(instance);
         // copy remaining stuff here
    }
}

is this an OK use of generics or not? I’m mostly thinking about the constraint to “itself”. I sometimes feel like generics can “explode” to other classes where I use the Base class.

  • 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-15T00:29:56+00:00Added an answer on May 15, 2026 at 12:29 am

    Yes, this is reasonable – I have something similar in my Protocol Buffers port (except more complicated, as there are two mutually referenced types).

    You’re absolutely right about generics sometimes ending up spreading across the code-base – what you’ve done here means that bits of code which only care about Derived having an appropriate API don’t need to worry about the generics.

    My advice is to try to keep it simple where possible, but in cases where an “odd” generic constraint really does describe what you want, go for it. You should be aware that this doesn’t force valid use though – you could easily have:

    class Banana : Base<Derived>
    

    and that would be valid, though odd and probably unexpected to users.

    You might also want to consider sealing Derived here – if you derive further, you’re again likely to end up with odd behavior (or at least an odd API).

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

Sidebar

Related Questions

After researching a bit how the different way people slugify titles, I've noticed that
i have philosophised about the purpose of stack a little bit and after some
After doing a bit of digging i found some (minor - in my opinion)
After I've seen this PDC session I wanted to try to learn a bit
after a bit of searching I was unable to find a helpful post so...
I'm after a bit of advice on how to handle a multi tenant site
After reading a bit on Javascript's prototypical inheritance model , I change my style
UPDATE: After searching a bit I don't seem to be alone about this problem:
Im skinning a progressBar in Flex, and after reading a bit about it, I
Edit: Prepare my objects for the use within a HashMap. after reading a bit

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.