How to define generic type limit to primitive types?

Use this: where C: struct That will limit it to value types EDIT: I just noticed you also mention string. Unfortunatley, strings won't be allowed as they are not value types.

Use this: where C: struct That will limit it to value types. EDIT: I just noticed you also mention string. Unfortunatley, strings won't be allowed as they are not value types.

But not for string which is nullable – David.Chu. Ca Apr 30 '09 at 3:47 1 And of course it lets you pass any user-defined struct type, not just primitive types. I'm not sure there's a way, really, other than defining overloads for all the built-in primitive types.

– Matt Hamilton Apr 30 '09 at 3:49.

You're looking for: T GetObject where T: struct.

Dang it! I hate when someone beats me to the punch. Nice BFree!

– Joshua Belden Apr 30 '09 at 3:48 how about string which is primitive but nullable type – David.Chu. Ca Apr 30 '09 at 3:49 1 @David: string isn't a primitive type. It's a reference type that in some cases is treated as a primitive type.

– Samuel Apr 30 '09 at 3:51.

It could be that you actually need C to implement IComparable, or someother interface. In which case you want something like T GetObject where T: IComparable.

There is no generic constraint that matches that set of things cleanly. What is it that you actually want to do? For example, you can hack around it with runtime checks, such as a static ctor (for generic types - not so easy for generic methods)... However; most times I see this, it is because people want one of: to be able to check items for equality: in which case use EqualityComparer.

Default to be able to compare/sort items: in which case use Comparer. Default to be able to perform arithmetic: in which case use MiscUtil's support for generic operators.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions