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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:05:22+00:00 2026-06-17T13:05:22+00:00

How could the typesafe enum pattern be implemented on a generic class? Let’s assume

  • 0

How could the typesafe enum pattern be implemented on a generic class? Let’s assume it’s implemented along these lines

public class KnownSetting<T>
{
    public readonly static KnownSetting<String> Name = new KnownSetting<String>("name", "Default Name", t => t);
    public readonly static KnownSetting<int> Size = new KnownSetting<String>("size", "25", t => Converter.ToInt32);

    public String Key { get; set; }
    public T DefaultValue { get; set; }
    public Func<String, T> Converter { get; set; }

    private KnownSetting(String key, T defaultValue, Func<String, T> converter)
    {
        Key = key;
        DefaultValue = defaultValue;
        Converter = converter;
    }
}

The implementation of the pattern is correct this way since the constructor remains private, but when using this construct, it looks wrong:

public static class Program
{
    public static void main()
    {
        var x = KnownSetting<?>.Name;
    }
}

Then an option would be to split it in two, KnownSetting container class and the Setting implementation, but then the scope of the constructor cannot be private so as to be instantiated from within the container.

How can this pattern be implemented so that the generics aspect of it stays concealed from the end-user, but remains strongly typed? Is there a more suitable pattern, or is there a better way to implement it?

Update
I added a second sample to illustrate that I do want the type of the setting to be generic.

  • 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-17T13:05:23+00:00Added an answer on June 17, 2026 at 1:05 pm

    Create a helper method in the base type that uses another type and create a known settings class. You need the Create method because the base constructor is Setting(string, object, Func). This is also why I introduce another generic variable (U):

    public class KnownSetting : Setting<object>
    {
        private KnownSetting(string key, object defaultValue, Func<string, object> converter) : base(key, defaultValue, converter) { }
    
        public readonly static Setting<string> Name = Create<string>("name", "Default Name", t => t);
        public readonly static Setting<int> Size = Create<int>("size", 25, t => Convert.ToInt32(t));
    }
    
    public class Setting<T>
    {
        public string Key { get; set; }
        public T DefaultValue { get; set; }
        public Func<string, T> Converter { get; set; }
    
        protected static Setting<U> Create<U>(string key, U defaultValue, Func<string, U> converter)
        {
            return new Setting<U>(key, defaultValue, converter);
        }
    
        protected Setting(string key, T defaultValue, Func<string, T> converter)
        {
            Key = key;
            DefaultValue = defaultValue;
            Converter = converter;
        }
    }
    public static class Program
    {
        static void Main(string[] args)
        {
            var x = KnownSetting.Name;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In java <1.5, constants would be implemented like this public class MyClass { public
Could this be done with javascript/jQuery : <div class=number>99,123,123</div> <div class=number>123,123,123</div> <div class=number>1,123,123,123</div> <div
Could you please clarify that why final keyword is required before class when we
Could someone please offer a concise explanation for the difference between these two Python
If a language is type-safe does that mean one could automatically assume that its
Suppose I have two classes deriving from a third abstract class: public abstract class
I have my own generic class which goes like this: C1<T> { T value;
Could someone know where is saved path to external XML-File that contain configuration for
Could anyone guide me in created a simple app in JS? What the app
Could someone please tell me why this <%= destroy_password_url @user.password_reset_token %> generates http://localhost:3000/api/destroy_password.4G5EoRVYMUAtiIKqOerKsw routes.rb

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.