471,337 Members | 817 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Trouble understanding compiler message on generic where clause

I have this method declaration:

private SomeType getSomething<T>( T spec ) where T: String, MemoryStream

The compiler error I get is:

'string' is not a valid constraint. A type used as a constraint must be
an interface, a non-sealed class or a type parameter.

I don't understand what is meant by "type parameter" in this context. I
understand a "type parameter" to be my "T" so I don't understand how a
constraint can be the same thing...
Jan 19 '06 #1
3 1604
Hello Brad,

For one, string is sealed so that's why it's not working.

Second, you could do something like this:

public class AClass<T>
{
private void Do<d>(d val) where d : T { }
}
--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net
I have this method declaration:

private SomeType getSomething<T>( T spec ) where T: String,
MemoryStream

The compiler error I get is:

'string' is not a valid constraint. A type used as a constraint must
be an interface, a non-sealed class or a type parameter.

I don't understand what is meant by "type parameter" in this context.
I understand a "type parameter" to be my "T" so I don't understand how
a constraint can be the same thing...

Jan 19 '06 #2
Brad Wood <bradley|.wood|@ndsu|.edu> wrote:
I have this method declaration:

private SomeType getSomething<T>( T spec ) where T: String, MemoryStream

The compiler error I get is:

'string' is not a valid constraint. A type used as a constraint must be
an interface, a non-sealed class or a type parameter.

I don't understand what is meant by "type parameter" in this context. I
understand a "type parameter" to be my "T" so I don't understand how a
constraint can be the same thing...


T is the type parameter, and "where T : String, MemoryStream" is the
constraint. However:

1) You can't have a constraint against two classes in the same way as
you can't derive from two classes

2) You can't constrain a type to have a "minimal base type" which is
sealed (like string)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 19 '06 #3
Patrik Löwendahl [C# MVP] wrote:
Second, you could do something like this:

public class AClass<T> {
private void Do<d>(d val) where d : T { }
}


Got it; a where constraint can include a reference to a type parameter
other than itself. Thanks.
Jan 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Daniel Walzenbach | last post: by
1 post views Thread by Anonieko | last post: by
reply views Thread by James Wong | last post: by
14 posts views Thread by James Wong | last post: by
3 posts views Thread by Greg Corradini | 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.