This is prohibited by C #, as described in section 4.4.3 of the Bound and Unbound Types specifications.
If the constraint is a constructor constraint new(), the type Ashould not be abstractand must have an open constructor without parameters. This is true if one of the following conditions is true.
A - value type, since all value types have a standard default constructorA is a type parameter having a cosntructor constraintA - ,A - , abstract public constuctorA abstract .
, - . , , , , , , .
accessor internal public . public private internal, .
internal class C<T> where : T new()
{
public C() : this(new T()) {
}
private C(T t) {
// Do additional initialization
}
}
, , private.
internal class C<T> where T : new() {
public C() {
T t = new T();
InitializeClass(t);
}
private void InitializeClass(T t) {
throw new NotImplementedException();
}
}
.
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;
}
}
Singleton<String> singleton = new Singleton<String>();
Singleton.Current.Name = "SoMoS";
, - .
public class Singleton<T> where T : new()
{
public static Singleton<T> Current {
get;
private set;
}
internal Singleton() {
T t = new T();
}
public String Name {
get;
set;
}
}
, . . , Factory Pattern, Injection ..