How to define generic type limit to primitive types?

C#.NetGenericsType Constraints

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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

Solution 3 - C#

Here's what you're looking for:

T GetObject<T>() where T : struct;

Solution 4 - C#

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

Solution 5 - C#

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

Solution 6 - C#

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

Solution 7 - C#

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://docs.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...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid.Chu.caView Question on Stackoverflow
Solution 1 - C#BFreeView Answer on Stackoverflow
Solution 2 - C#DekoView Answer on Stackoverflow
Solution 3 - C#Joshua BeldenView Answer on Stackoverflow
Solution 4 - C#Marc GravellView Answer on Stackoverflow
Solution 5 - C#David McEwingView Answer on Stackoverflow
Solution 6 - C#Jean-Xavier BardantView Answer on Stackoverflow
Solution 7 - C#YogurtuView Answer on Stackoverflow