Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic constraint to match numeric types [duplicate]

I'm trying to write an extension method on numeric types to be used in a fluent testing framework I'm building. Basically, I want to do this:

public static ShouldBeGreaterThan<T>(this T actual, T expected, string message)     where T : int || T: double || etc... 

Just where T : struct doesn't do, since that will also match string and bool, and possibly something else I'm forgetting. is there something I can do to match only numeric types? (Specifically types that implement the > and < operators, so I can compare them... If this means I'm matching dates as well, it doesn't really matter - the extension will still do what I expect.)

like image 566
Tomas Aschan Avatar asked Jul 25 '10 14:07

Tomas Aschan


People also ask

Can generic classes be constrained?

Declaring those constraints means you can use the operations and method calls of the constraining type. If your generic class or method uses any operation on the generic members beyond simple assignment or calling any methods not supported by System. Object, you'll apply constraints to the type parameter.

What does the generic constraint of type interface do?

Interface Type Constraint You can constrain the generic type by interface, thereby allowing only classes that implement that interface or classes that inherit from classes that implement the interface as the type parameter.

How do I compare generic types in C#?

To enable two objects of a generic type parameter to be compared, they must implement the IComparable or IComparable<T>, and/or IEquatable<T> interfaces. Both versions of IComparable define the CompareTo() method and IEquatable<T> defines the Equals() method.


2 Answers

In this case you want to constrain your generic to the IComparable interface, which gives you access to the CompareTo method, since this interface allows you to answer the question ShouldBeGreaterThan.

Numeric types will implement that interface and the fact that it also works on strings shouldn't bother you that much.

like image 79
flq Avatar answered Oct 13 '22 06:10

flq


where T : struct,            IComparable,            IComparable<T>,            IConvertible,            IEquatable<T>,            IFormattable 

That's the closest I can get to a numeric constraint. All the numeric types implement these 5 interfaces, but IFormattable is not implemented by bool, and strings are a reference type, so they're not applicable.

There's some other things that implement these - DateTime for example, so it's not really as required, but prevents a lot of instantiations you don't want.

like image 25
Mark H Avatar answered Oct 13 '22 05:10

Mark H