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).
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).
where T : new() Means that the type T must have a parameter-less constructor. Having this constraint will allow you to do something like T field = new T(); in your code which you wouldn't be able to do otherwise.
Constructors can be marked as public, private, protected, internal, protected internal or private protected.
internal constructor are good when you don't want the client to create the instance. You'd usually want to do it if you want to control how the instance should be created.
The new constraint specifies that a type argument in a generic class or method declaration must have a public parameterless constructor.
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 typeAmust not beabstractand must have a public parameterless constructor. This is satisfied if one of the following is true.
Ais a value type, since all value types have a public default constructorAis a type parameter having the cosntructor constraintAis a type parameter having the value type constraintAis a class that is notabstractand contains an explicitly declaredpublicconstuctor with no parametersAis notabstractand 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..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With