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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:44:18+00:00 2026-05-14T23:44:18+00:00

I’ve been dealing a lot lately with abstract classes that use generics. This is

  • 0

I’ve been dealing a lot lately with abstract classes that use generics. This is all good and fine because I get a lot of utility out of these classes but now it’s making for some rather ugly code down the line. For example:

abstract class ClassBase<T>
{
    T Property { get; set; }
}

class MyClass : ClassBase<string>
{
    OtherClass PropertyDetail { get; set; }
}

This implementation isn’t all that crazy, except when I want to reference the abstract class from a helper class and then I have to make a list of generics just to make reference to the implemented class, like this below.

class Helper
{
    void HelpMe<C, T>(object Value)
        where C : ClassBase<T>, new()
    {
        DoWork();
    }
}

This is just a tame example, because I have some method calls where the list of where clauses end up being 5 or 6 lines long to handle all of the generic data. What I’d really like to do is

class Helper
{
    void HelpMe<C>(object Value)
        where C : ClassBase, new()
    {
        DoWork();
    }
}

but it obviously won’t compile. I want to reference ClassBase without having to pass it a whole array of generic classes to get the function to work, but I don’t want to reference the higher level classes because there are a dozen of those. Am I the victim of my own cleverness or is there an avenue that I haven’t considered yet?

  • 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-14T23:44:19+00:00Added an answer on May 14, 2026 at 11:44 pm

    I suppose that your HelpMe method would be used for initializing the concrete ClassBase<T> type (a guess based on the constraints). To keep the code fully generic (if you need both T and C somewhere in the method), you probably need to keep both of the type parameters.

    However, you could add a non-generic base class and then write something like this:

    abstract class ClassBase { 
       object UntypedProperty { get; set; } 
    } 
    abstract class ClassBase<T> : ClassBase { 
       T Property { get; set; } 
       public override object UntypedProperty { 
         get { return Property; }
         set { Property = (T)value; }
       }
    } 
    

    Then you could be to write the helper method like this:

    void HelpMe<C>(object Value) where C : ClassBase, new() { 
      var n = new C();
      c.UntypedProperty = Value;
    } 
    

    Depending on your specific scenario, something along these lines might work and make the code a little bit simpler. However, you need to modify the base class to make this possible.

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

Sidebar

Related Questions

No related questions found

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.