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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:16:34+00:00 2026-06-06T01:16:34+00:00

Suppose I have a Generic abstract class that provides some default constructor functionality so

  • 0

Suppose I have a Generic abstract class that provides some default constructor functionality so you don’t have to repeat the code in your inherited classes:

public abstract class Base<TKey, TValue>
{
    private static readonly IDictionary<TKey, Base<TKey, TValue>> Values = new Dictionary<TKey, Base<TKey, TValue>>();

    public TKey Key { get; private set; }
    public TValue Value { get; private set; }

    protected Base(TKey key, TValue value)
    {
        this.Key = key;
        this.Value = value;

        Values.Add(key, this);
    }
}

Now the problem is that when I write two inherited classes like:

public sealed class One : Base<string, string>
{
    public static readonly One Default = new One("DEF", "Default value");
    public static readonly One Invalid = new One("", "Invalid value");

    private One(string key, string value) : base(key, value) {}
}

public sealed class Two : Base<string, string>
{
    public static readonly Two EU = new Two("EU", "European Union");
    public static readonly Two USA = new Two("", "United States of America");

    private Two(string key, string value) : base(key, value) {}
}

As you can see these two are sort of type-safe enumerations really. They only provide predefined values and they can’t be instantiated for any other purpose.

Question

The problem is because both of these inherited classes use the same generic types with base class which is Base<string, string>. And the way that generic classes and static fields work is that for each generic type (or combination when more than one) a new static field is created.

In my case the combination is string, string that’s why there’s only one base class static field and it holds 4 values instead of having two static fields each with 2 values.

How do I overcome this issue and separate those while still keeping this functionality on the base class so I don’t repeat my code?

Changing field from static to instance won’t work, because they I’ll end up with 4 instances each holding just one value…

  • 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-06T01:16:35+00:00Added an answer on June 6, 2026 at 1:16 am

    This will work. I believe it’s known as the curiously recurring template pattern, but don’t quote me on that

    public abstract class Base<TSelf, TKey, TValue>
    {
        private static readonly IDictionary<TKey, Base<TSelf, TKey, TValue>> Values = 
            new Dictionary<TKey, Base<TSelf, TKey, TValue>>();
    
        public TKey Key { get; private set; }
        public TValue Value { get; private set; }
    
        protected Base(TKey key, TValue value)
        {
            this.Key = key;
            this.Value = value;
    
            Values.Add(key, this);
        }
    }
    
    
    public sealed class One : Base<One, string, string>
    {
        public static readonly One Default = new One("DEF", "Default value");
        public static readonly One Invalid = new One("", "Invalid value");
    
        private One(string key, string value) : base(key, value) { }
    }
    
    public sealed class Two : Base<Two, string, string>
    {
        public static readonly Two EU = new Two("EU", "European Union");
        public static readonly Two USA = new Two("", "United States of America");
    
        private Two(string key, string value) : base(key, value) { }
    }
    

    The ‘curious’ part is that the definitions of One and Two are allowed to use themselves as type parameters to themselves!

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

Sidebar

Related Questions

Suppose you have the following Generic class heirarchy: public abstract class GenericBase<T> { T
I have a generic class for all the managers that are in my framework.
Suppose you have a class that is frequently (or even exclusively ) used as
Which is your preference? Let's say we have a generic Product table that has
Suppose we have a nested generic class: public class A<T> { public class B<U>
Suppose I have a generic class called MyClass<T> how can I create a JavascriptConverter
Suppose I have some generic function genericFunc :: a -> b genericFunc x =
Suppose I have written a decorator that does something very generic. For example, it
Suppose I have a generic class with a generic parameter T which is a
Suppose I have the following class: using System; using System.Collections.Generic; using System.Linq; using System.Text;

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.