How to define generic type limit to primitive types?
Asked Answered
R

7

100

I have the following method with generic type:

T GetValue<T>();

I would like to limit T to primitive types such as int, string, float but not class type. I know I can define generic for class type like this:

C GetObject<C>() where C: class;

I am not sure if it is possible for primitive types and how if so.

Rebarebah answered 30/4, 2009 at 3:42 Comment(2)
I think that by "primitive types" @Rebarebah means unmanaged types, i.e. int, float etc. not Int32, Int64, Single etc.. Although class denotes a ref type, "but not class type" is suggesting the distinction between unmanaged and managed primitives.Bandeau
People run into this often. Its worth noting that many of the upvoted answers suggest adding constraints that describe Interface behaviors on T. Heed those answers, because they are good guidance. In 90% of the cases, when you think you need T to be constrained to primitive, what you might really be running into is the desire to perform certain operations on T. For instance, some operations like comparing T==T have non-obvious solutions. T==T is performed with this: EqualityComparer<T>.Default.Equals(a, b). Even though that line is hideous, it compiles to very fast code.Rox
C
59

You can use this to limit it to value types:

where C: struct

You also mention string. Unfortunately, strings won't be allowed as they are not value types.

Chancelor answered 30/4, 2009 at 3:47 Comment(4)
but not for string which is nullableRebarebah
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.Sapheaded
The question was for primitives, not value types. As you mention in your answer the string primitive is not a value type, so it doesn't work in that regard -- also it will allow you to pass random structs (as @MattHamilton points out) which are not primitives.Escalera
With this if I have a method that does n1 + n2 where n1 and n2 both are of type C, it is throwing me the error: Operator '+' cannot be applied to operands of type 'C' and 'C'Holothurian
M
32

Actually this does the job to certain extend:

public T Object<T>() where T :
   struct, IComparable, IFormattable, IConvertible, IComparable<T>, IEquatable<T>

To limit to numeric types you can get some useful hints of the following samples defined for the ValueType class

Moorefield answered 3/12, 2013 at 12:37 Comment(1)
Note: in case you are using this to feed ID field to database, same as me, Guid struct does not support IConvertible interface, and you may want to remove it.Calle
P
18

Here's what you're looking for:

T GetObject<T>() where T : struct;
Pindling answered 30/4, 2009 at 3:48 Comment(6)
Dang it! I hate when someone beats me to the punch. Nice BFree!Pindling
how about string which is primitive but nullable typeRebarebah
@David: string isn't a primitive type. It's a reference type that in some cases is treated as a primitive type.Blida
But there's no way to cast a dynamically-retrieved objects to meet that struct requirement, is there?Morpho
@Morpho This post might help,#31374005.Pindling
String is a primitive but not a struct / value type.Escalera
B
11

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<T>.Default
  • to be able to compare/sort items: in which case use Comparer<T>.Default
  • to be able to perform arithmetic: in which case use MiscUtil's support for generic operators
Bog answered 30/4, 2009 at 4:26 Comment(1)
@MehdiDehghani nuget.org/packages/JonSkeet.MiscUtil, jonskeet.uk/csharp/miscutilBog
R
10

What are you actually trying to do in the method? It could be that you actually need C to implement IComparable, or someother interface. In which case you want something like

T GetObject<T> where T: IComparable
Rexanne answered 30/4, 2009 at 3:58 Comment(0)
O
4

I'm under the same need, I want to create a method that should retrieve a List where T should be a primitive type like int, double, decimal, etc...

Based on this Microsoft documentation: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/unmanaged-types

Looks like the right approach is to use

where T : unmanaged

quoting:

A type is an unmanaged type if it's any of the following types:

sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool Any enum type Any pointer type Any user-defined struct type that contains fields of unmanaged types only and, in C# 7.3 and earlier, is not a constructed type (a type that includes at least one type argument)

Also important quote:

Beginning with C# 7.3, you can use the unmanaged constraint to specify that a type parameter is a non-pointer, non-nullable unmanaged type.

Beginning with C# 8.0, a constructed struct type that contains fields of unmanaged types only is also unmanaged...

Oby answered 18/4, 2022 at 22:30 Comment(2)
To get around C# 8.0's expansion in seeing structs as unmanaged, you can probably still restrict via the unmanaged, IComparable, IFormattable, IConvertible, IComparable<T>, IEquatable<T> yadda as before. I wish primitives would actually implement something allowing to uniquely recognize them (like IPrimitive that cannot be implemented by others)...Frazil
unmanaged does not contain the string type, just need add an overload for string.Nuthatch
C
0

If you need types for which you can use languages features reserved for managed types such as the sizeof operator, use "unmanaged".

where C: unmanaged
Cavatina answered 28/12, 2021 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.