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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:25:48+00:00 2026-05-20T12:25:48+00:00

I have a abstract class which represents an immutable value. Like strings, the immutable

  • 0

I have a abstract class which represents an immutable value. Like strings, the immutable class has methods which appear to change the instance, but really just return a new instance with the changes. The abstract class has some of these methods which have some default behavior. Can I have this kind of Create modified instance and return logic in the abstract class? I’m getting hung up because I don’t know what to put in the ??? positions. It seems like what I need is a type T that represents the deriving type. Are there any such patterns out there to overcome this problem?

public abstract class BaseImmutable
{
    public readonly object Value;

    protected BaseImmutable(object val)
    {
        Value = val;
    }

    public ??? ModifiyButNotReally()
    {
        object newValue = GetNewObjectFromOld(Value);
        return new ???(newValue);
    }
}

public class DerivedImmutable : BaseImmutable
{
    public DerivedImmutable(object val) : base(val)
    {
    }
}

ANSWER

This was easier than I suspected. A method can be made generic without making the class generic. With that in mind the only thing we have to do is constrain the generic argument to inherit from the base class and require it not be abstract by including a new() constraint. One last step I needed to do was to use reflection to get the non-parameterless constructor to create a new instance. I found this solution to be better than the generic class answer suggested by @Jason, which I have since learned is known as the Curiously Recurring Template Pattern. It also avoids the need to create a difficult-to-test/moq extension method in a separate static class as suggested by @bottleneck. However, @bottleneck’s answer is what gave me the inspiration for the solution so the points go to him.

public abstract class BaseImmutable
{
    public readonly object Value;

    protected BaseImmutable(object val)
    {
        Value = val;
    }

    public T ModifiyButNotReally<T>() where T : BaseImmutable, new()
    {
        object newValue = GetNewObjectFromOld(Value);
        var ctor = typeof(T).GetConstructor(new[] { typeof(object) });
        return (T)ctor.Invoke(new object[] { newValue });
    }
}

public class DerivedImmutable : BaseImmutable
{
    public DerivedImmutable(object val) : base(val)
    {
    }
}
  • 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-20T12:25:48+00:00Added an answer on May 20, 2026 at 12:25 pm

    Maybe an extension method would be in order here?

    public static T Modify<T>(this T immutable) where T:BaseImmutable{
        //work your magic here
        //return T something
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a abstract class which has some common methods for all of my
JavaScript is a prototyped-based language, and yet it has the ability to mimic some
I am writing an application to compose musical freizes, but I am a little
I'm using PHP 5.3 and SimpleTest, but more general answers are welcome. Each plug-in
I'll start by saying that over the course of a few projects, I have
I'm curious as to see how most developers go about designing the contracts to
Objective To write a effecient Active Directory library to ease the work of technicals
I'm working on a evolutionary simulation model implemented in Java and ran into a
I've seen various MVC frameworks as well as standalone ORM frameworks for PHP, as
I haven't got problems with code since my last problem with interface covariance. Today

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.