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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:14:12+00:00 2026-06-03T23:14:12+00:00

I’ve created a generic class that needs to instantiate its implementation type, so the

  • 0

I’ve created a generic class that needs to instantiate its implementation type, so the implementation type has to have an accessible parameter less constructor. Looks like the new() restriction could do the job but its enforcing the implementation type to have a public constructor when I have it internal (but accessible as both are on the same assembly).

  1. Is there a reason to force it to be public rather than “accessible”?
  2. Is there a way to do what I need?

Thanks in advance.

EDIT: The reason to do this is that I have a class X that must be used through a Singleton. The Singleton class is a generic class and I want to make the class X constructor internal to avoid external users accessing the object in a wrong way (calling the constructor).

  • 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-03T23:14:13+00:00Added an answer on June 3, 2026 at 11:14 pm

    This is disallowed by the C# language as outlined in section 4.4.3 of the specification Bound and Unbound Types.

    If the constraint is the constructor constraint new(), the type A must not be abstract and must have a public parameterless constructor. This is satisfied if one of the following is true.

    • A is a value type, since all value types have a public default constructor
    • A is a type parameter having the cosntructor constraint
    • A is a type parameter having the value type constraint
    • A is a class that is not abstract and contains an explicitly declared public constuctor with no parameters
    • A is not abstract and has a default constructor.

    A compiler error if any one of these conditions are not met. If you find yourself having types that are public but only with internal constructors, then most likely they should actually be internal types with public constructors.

    I would recommend changing your types accessor to internal and its constructor’s to public, and make it parameterless. Your public parameterless constructor could then call through to a non-parameterless private or internal constructor to do any additional initialization work.

    internal class C<T> where : T new()
    {
        public C() : this(new T()) {
        }
    
        private C(T t) {
            // Do additional initialization
        }
    }
    

    Mind you that pattern is limited, but there’s nothing stopping you from using a private method instead.

    internal class C<T> where T : new() {
        public C() {
            T t = new T();
            InitializeClass(t);
        }
    
        private void InitializeClass(T t) {
            throw new NotImplementedException();
        }
    }   
    

    As per your update, here’s a small example of a public singleton patter.

    public class Singleton<T> where T : new()
    {
        public static Singleton<T> Current {
            get;
            private set;
        }
    
        internal Singleton() : this(new T()) {
        }
    
        private Singleton(T t) {
            Current = this;
            // Do whatever you need to with T
        }        
    
        public String Name {
            get;
            set;
        }
    }
    

    Usage

    // Somewhere in your internal assembly
    Singleton<String> singleton = new Singleton<String>();
    
    // In an external assembly
    Singleton.Current.Name = "SoMoS";
    

    You don’t even need to use constructors in that fashion either, you can just as easily do something simple.

    public class Singleton<T> where T : new()
    {
        public static Singleton<T> Current {
            get;
            private set;
        }
    
        internal Singleton() {
            T t = new T();
            // Do stuff with T
        }
    
        public String Name {
            get;
            set;
        }
    }
    

    Generics may not be the way to go if you can’t design it to fit your requirements. Generics can only do so much and don’t solve every problem. There are things like Factory Pattern, Injection, etc..

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.