471,306 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

Why where new() doesn't allow parameters?

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?

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 28 '05 #1
4 2160
> 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<ClassWithStrin gCtor> OK");
try
{
ConstrainedClass<ClassWithoutStringCtor> c=new ConstrainedClass<ClassWithoutStringCtor>();
}
catch (Exception)
{
Console.WriteLine("ConstrainedClass<ClassWithoutSt ringCtor> 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/
Nov 28 '05 #2
Anders Norås wrote:
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.


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.
</quote>
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:


<snip>

Hmm. Not exactly pleasant though, is it?

Jon

Nov 28 '05 #3
> 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/
Nov 28 '05 #4
> No, but it's the only way to do it. I have found a need to constrain
constructor signatures this way in real code...

That should of coure be "I have *never* found a need"... :)

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 28 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Jordan Rastrick | last post: by
822 posts views Thread by Turamnvia Suouriviaskimatta | last post: by
1 post views Thread by Foxie Foxster | last post: by
4 posts views Thread by Robert Schuldenfrei | last post: by
6 posts views Thread by Robert Schuldenfrei | last post: by
1 post views Thread by Crercio O. Silva | last post: by
11 posts views Thread by =?Utf-8?B?QWNoaWxsZXNfX19f?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.