471,319 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Value / primitive type constraints on generics

Is it possible to constrain a generic to be a value or primitive type?

(Any kludges/workarounds welcome if the immediate answer is no....)
Jan 10 '06 #1
12 6242
Dave,

Yes, it is. You would do this:

public class MyClass<T> where T : struct

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dave Booker" <db******@newsgroup.nospam> wrote in message
news:86**********************************@microsof t.com...
Is it possible to constrain a generic to be a value or primitive type?

(Any kludges/workarounds welcome if the immediate answer is no....)

Jan 10 '06 #2
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uz*************@TK2MSFTNGP10.phx.gbl...
public class MyClass<T> where T : struct


Does "where T : struct" work for an int too?

-- Alan
Jan 10 '06 #3
Alan,

You mean for T being an int? Yes, it does.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alan Pretre" <no@spam> wrote in message
news:eG**************@TK2MSFTNGP11.phx.gbl...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:uz*************@TK2MSFTNGP10.phx.gbl...
public class MyClass<T> where T : struct


Does "where T : struct" work for an int too?

-- Alan

Jan 10 '06 #4
"Alan Pretre" <no@spam> wrote in news:eG1jriiFGHA.1032
@TK2MSFTNGP11.phx.gbl:
Does "where T : struct" work for an int too?


What would be the point of using Generics if you are specifying the exact
type?

-mdb
Jan 10 '06 #5
Michael,

What I think he meant to ask was will the constraint using "struct"
allow an int to be passed for the type parameter T.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Michael Bray" <mbray@makeDIntoDot_ctiusaDcom> wrote in message
news:Xn****************************@207.46.248.16. ..
"Alan Pretre" <no@spam> wrote in news:eG1jriiFGHA.1032
@TK2MSFTNGP11.phx.gbl:
Does "where T : struct" work for an int too?


What would be the point of using Generics if you are specifying the exact
type?

-mdb

Jan 10 '06 #6
"Michael Bray" <mbray@makeDIntoDot_ctiusaDcom> wrote in message
news:Xn****************************@207.46.248.16. ..
What would be the point of using Generics if you are specifying the exact
type?


On the surface I don't exactly see how an int is equivalent to a struct,
except in the broad sense that they are both value types. Frankly, I'm
surprised that that works.

Sorry if I seem lazy but I'm not in a position to test out an example on my
own at this moment.

-- Alan
Jan 10 '06 #7
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
news:#5**************@tk2msftngp13.phx.gbl:
What I think he meant to ask was will the constraint using "struct"
allow an int to be passed for the type parameter T.


Yeah I realized that after I saw your response. :) That's why I'm eating
lunch at 4:30.

-mdb
Jan 10 '06 #8
Alan Pretre <no@spam> wrote:
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uz*************@TK2MSFTNGP10.phx.gbl...
public class MyClass<T> where T : struct


Does "where T : struct" work for an int too?


Absolutely - an int is a struct.

--
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 10 '06 #9
Sadly, you can't constrain a generic to be an Enum.

public class MyClass<T> where T : Enum

:(

Jan 10 '06 #10
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Absolutely - an int is a struct.


That's a nuance I wasn't aware of. Thanks all.

-- Alan
Jan 10 '06 #11
Alan Pretre <no@spam> wrote:
"Michael Bray" <mbray@makeDIntoDot_ctiusaDcom> wrote in message
news:Xn****************************@207.46.248.16. ..
What would be the point of using Generics if you are specifying the exact
type?


On the surface I don't exactly see how an int is equivalent to a struct,
except in the broad sense that they are both value types. Frankly, I'm
surprised that that works.


In what way is it *not* a struct? Heck, if you look in MSDN it's even
documented with a struct declaration:

[SerializableAttribute]
[ComVisibleAttribute(true)]
public struct Int32 : IComparable, IFormattable, IConvertible,
IComparable<int>, IEquatable<int>

Now, I agree that it's different in terms of there being built-in
support for it in the CLR, *and* in terms of it being in some sense
"atomic" - but it works like a perfectly normal struct in general.

--
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 10 '06 #12
Hi ,

I just wanted to check how things are going. If there is any question,
please feel free to join the community and we are here to support you at
your convenience. Thanks again and have a nice day!

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Jan 13 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by DaKoadMunky | last post: by
11 posts views Thread by andrew queisser | last post: by
5 posts views Thread by Zach | last post: by
49 posts views Thread by matty | last post: by
5 posts views Thread by howa | 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.