Why where new() doesn't allow parameters?

  • Thread starter Thread starter =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=
  • Start date Start date
?

=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

Ok, I realize that it doesn't really matter because it simply isn't
possible, but does anyone know the reason why the constraint on the
generic parameters for new() doesn't allow parameters?

Ie. why isn't this allowed:

public class Collection<T> where T: SomeBaseClass, new(Session)

to make sure the type has a constructor with a Session parameter?
 
Ie. why isn't this allowed:
public class Collection<T> where T: SomeBaseClass, new(Session)
The new() constraint is used to ensure that a new instance of the class can
be created, and not the signature of the generic parameter's constructor.
Se §20.8.2 of the C# 2.0 spec for more info.
to make sure the type has a constructor with a Session parameter?
You can do this by using a static constructor on your generic type. The following
example constrains the generic T parameter to have a constructor that accepts
a single string argument:
public class ConstrainedCtorExample
{
public static void Main()
{
ConstrainedClass<ClassWithStringCtor> c=new ConstrainedClass<ClassWithStringCtor>();
Console.WriteLine("ConstrainedClass<ClassWithStringCtor> OK");
try
{
ConstrainedClass<ClassWithoutStringCtor> c=new ConstrainedClass<ClassWithoutStringCtor>();
}
catch (Exception)
{
Console.WriteLine("ConstrainedClass<ClassWithoutStringCtor> OK");
}
}
}
public class ClassWithStringCtor
{
public ClassWithStringCtor(string s)
{
}
}
public class ClassWithoutStringCtor
{
public ClassWithoutStringCtor()
{
}
}
public class ConstrainedClass<T>
{
static ConstrainedClass()
{
if (typeof(T).GetConstructor(new Type[] {typeof(string)}) == null)
{
throw new Exception(string.Format("The type '{0}' does not have a constructor
accepting a string argument",typeof(T).Name));
}
}
}

Regards,
Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Anders said:
The new() constraint is used to ensure that a new instance of the class can
be created, and not the signature of the generic parameter's constructor.

No, it ensures that there's a public *parameterless* constructor.
Se §20.8.2 of the C# 2.0 spec for more info.

I call your 20.8.2 and raise you a 20.7.1:

<quote>
If the constraint is new(), the type argument A must not be abstract
and must have a public parameterless constructor.
You can do this by using a static constructor on your generic type. The following
example constrains the generic T parameter to have a constructor that accepts
a single string argument:

<snip>

Hmm. Not exactly pleasant though, is it?

Jon
 
No, it ensures that there's a public *parameterless* constructor.
Ok. I hear ya'!
Hmm. Not exactly pleasant though, is it?
No, but it's the only way to do it. I have found a need to constrain constructor
signatures this way in real code, but I've used it on several occations to
ensure that a generic parameter is serializable. Eg:
static MyClass()
{
if (!typeof(T).IsSerializable)
{
// throw exception
}
}

Regards,
Anders Norås
http://dotnetjunkies.com/weblog/anoras
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top