By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,771 Members | 1,448 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,771 IT Pros & Developers. It's quick & easy.

generic with constraint of type int

P: n/a
I have a class that i need a constraint of int, string, float or bool. I
have tried the following but can't make VS accept it. I read the docs
and they showed that any value type can be used unless it is nullable.
Why doesn't this work?

public class Metadata<T>
where T: System.int
line from the docs
where T: struct
The type argument must be a value type. Any value type except Nullable
can be specified. See Using Nullable Types (C# Programming Guide) for
more information.
dan
May 16 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
It means exactly what it says. If you have class Metadata<T> where T: struct

then you can have

Metadata<int>
Metadata<float>
Metadata<bool>
etc.
but you would not be able to have e.g. Metadata<Nullable<int>>,
neither would you be able to have Metadata<string> because string is a
reference type.

You would not be able to have a constraint which restricts to only int,
float, string and bool. The legal values for constraints are listed here:
http://msdn2.microsoft.com/en-us/lib...70(VS.80).aspx

======================
Clive Dixon
Digita Ltd. (www.digita.com)
"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
I have a class that i need a constraint of int, string, float or bool. I
have tried the following but can't make VS accept it. I read the docs and
they showed that any value type can be used unless it is nullable. Why
doesn't this work?

public class Metadata<T>
where T: System.int
line from the docs
where T: struct
The type argument must be a value type. Any value type except Nullable
can be specified. See Using Nullable Types (C# Programming Guide) for more
information.
dan

May 16 '06 #2

P: n/a
Dan Holmes <da*******@bigfoot.com> wrote:
I have a class that i need a constraint of int, string, float or bool.
You cannot express such a constraint using generic constraints. You'll
need to put an imperative test in the constructor.

Do for example:

---8<---
class A<T>
{
public A()
{
if (!(T is Int32) || // ... etc.
}
}
--->8---
Why doesn't this work?

public class Metadata<T>
where T: System.int
A new type cannot descend from an existing value type, so this
constraint would restrict the generic type to exactly one possible
instantiation, Metadata<int>.
line from the docs
where T: struct
The type argument must be a value type. Any value type except Nullable
can be specified. See Using Nullable Types (C# Programming Guide) for
more information.


This documentation is describing the literal constraint 'where T:
struct'. The 'struct' in this constraint description is not a
placeholder. The 'where T: struct' constraint constrains the type
parameter to be a value type other than nullable.

-- Barry
May 16 '06 #3

P: n/a
Barry Kelly <ba***********@gmail.com> wrote:
if (!(T is Int32) || // ... etc.


Correction:

if (typeof(T) != typeof(Int32) && // ... etc.

-- Barry
May 16 '06 #4

P: n/a
What is the best way to handle this? Check the type of T in the
constructor and throw if T isn't one of the valid types?

dan
Clive Dixon wrote:
It means exactly what it says. If you have class Metadata<T> where T: struct

then you can have

Metadata<int>
Metadata<float>
Metadata<bool>
etc.
but you would not be able to have e.g. Metadata<Nullable<int>>,
neither would you be able to have Metadata<string> because string is a
reference type.

You would not be able to have a constraint which restricts to only int,
float, string and bool. The legal values for constraints are listed here:
http://msdn2.microsoft.com/en-us/lib...70(VS.80).aspx

======================
Clive Dixon
Digita Ltd. (www.digita.com)
"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
I have a class that i need a constraint of int, string, float or bool. I
have tried the following but can't make VS accept it. I read the docs and
they showed that any value type can be used unless it is nullable. Why
doesn't this work?

public class Metadata<T>
where T: System.int
line from the docs
where T: struct
The type argument must be a value type. Any value type except Nullable
can be specified. See Using Nullable Types (C# Programming Guide) for more
information.
dan


May 16 '06 #5

P: n/a
That's the only way I can see of doing what you want, though it isn't ideal
since you're only catching at runtime and not at compile time.

--
======================
Clive Dixon
Digita Ltd. (www.digita.com)
"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:%2*****************@TK2MSFTNGP05.phx.gbl...
What is the best way to handle this? Check the type of T in the
constructor and throw if T isn't one of the valid types?

dan
Clive Dixon wrote:
It means exactly what it says. If you have class Metadata<T> where T:
struct

then you can have

Metadata<int>
Metadata<float>
Metadata<bool>
etc.
but you would not be able to have e.g. Metadata<Nullable<int>>,
neither would you be able to have Metadata<string> because string is a
reference type.

You would not be able to have a constraint which restricts to only int,
float, string and bool. The legal values for constraints are listed here:
http://msdn2.microsoft.com/en-us/lib...70(VS.80).aspx

======================
Clive Dixon
Digita Ltd. (www.digita.com)
"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
I have a class that i need a constraint of int, string, float or bool. I
have tried the following but can't make VS accept it. I read the docs
and they showed that any value type can be used unless it is nullable.
Why doesn't this work?

public class Metadata<T>
where T: System.int
line from the docs
where T: struct
The type argument must be a value type. Any value type except Nullable
can be specified. See Using Nullable Types (C# Programming Guide) for
more information.
dan


May 16 '06 #6

P: n/a
A better way to do it (although not ideal, because like the other it is
not caught at compile time) would be to check the type in the static
constructor, since that is done only once.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Clive Dixon" <cl*******************@digita.noluncheonmeat.com > wrote in
message news:el**************@TK2MSFTNGP02.phx.gbl...
That's the only way I can see of doing what you want, though it isn't
ideal since you're only catching at runtime and not at compile time.

--
======================
Clive Dixon
Digita Ltd. (www.digita.com)
"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:%2*****************@TK2MSFTNGP05.phx.gbl...
What is the best way to handle this? Check the type of T in the
constructor and throw if T isn't one of the valid types?

dan
Clive Dixon wrote:
It means exactly what it says. If you have class Metadata<T> where T:
struct

then you can have

Metadata<int>
Metadata<float>
Metadata<bool>
etc.
but you would not be able to have e.g. Metadata<Nullable<int>>,
neither would you be able to have Metadata<string> because string is a
reference type.

You would not be able to have a constraint which restricts to only int,
float, string and bool. The legal values for constraints are listed
here:
http://msdn2.microsoft.com/en-us/lib...70(VS.80).aspx

======================
Clive Dixon
Digita Ltd. (www.digita.com)
"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
I have a class that i need a constraint of int, string, float or bool.
I have tried the following but can't make VS accept it. I read the
docs and they showed that any value type can be used unless it is
nullable. Why doesn't this work?

public class Metadata<T>
where T: System.int
line from the docs
where T: struct
The type argument must be a value type. Any value type except Nullable
can be specified. See Using Nullable Types (C# Programming Guide) for
more information.
dan

May 16 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.