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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:53:14+00:00 2026-05-22T16:53:14+00:00

Example: I’d like to have several specialized textboxes that derive from either TextBox or

  • 0

Example:

I’d like to have several specialized textboxes that derive from either TextBox or RichTextBox, which both derive from TextBoxBase:

class CommonFeatures<T> : T where T : TextBoxBase
{
  // lots of features common to the TextBox and RichTextBox cases, like
    protected override void OnTextChanged(TextChangedEventArgs e) 
    { 
        //using TextBoxBase properties/methods like SelectAll();  
    }
}

and then

class SpecializedTB : CommonFeatures<TextBox>
{
    // using properties/methods specific to TextBox
    protected override void OnTextChanged(TextChangedEventArgs e) 
    { 
        ... base.OnTextChanged(e); 
    }
}

and

class SpecializedRTB : CommonFeatures<RichTextBox>
{
    // using methods/properties specific to RichTextBox
}

Unfortunately

class CommonFeatures<T> : T where T : TextBoxBase

doesn’t compile (“Cannot derive from ‘T’ because it is a type parameter”).

Is there a good solution to this? Thanks.

  • 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-22T16:53:15+00:00Added an answer on May 22, 2026 at 4:53 pm

    C# generics don’t support inheritance from a parameter type.

    Do you really need CommonFeatures to derive from TextBoxBase?

    A simple workaround may be to use aggregation instead of inheritance. So that you would have something like this:

    public class CommonFeatures<T> where T : TextBoxBase
    {
        private T innerTextBox;
    
        protected CommonFeatures<T>(T inner)
        {
            innerTextBox = inner;
            innerTextBox.TextChanged += OnTextChanged;
        }
    
        public T InnerTextBox { get { return innerTextBox; } }
    
        protected virtual void OnTextChanged(object sender, TextChangedEventArgs e) 
        { 
            ... do your stuff            
        }
    }
    

    Like @oxilumin says, extension methods may also be a great alternative if you don’t really need CommonFeatures to be a TextBoxBase.

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

Sidebar

Related Questions

Example: I have several types, e.g. Wheel, Brake, Engine, Clutch, AutoBox, ManualBox, ElectricWindow, RearParkingSensor,
Example, I want to specialize a class to have a member variable that is
Example: I have 10 view controllers, which are all allocated and initialized in the
example page I have a floating menu that i've built to the left side
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Example: You have a shortcut s to SomeProgram in the current directory. In cmd.exe
Example I have Person , SpecialPerson , and User . Person and SpecialPerson are
Example: select ename from emp where hiredate = todate('01/05/81','dd/mm/yy') and select ename from emp
Example in VB I have a worker class Public Class worker Public Event EventNumber1(ByVal
Example: var sessions = require('sessions'); function load_session(req) { sessions.load(sid); } Now that the session

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.