I would like to create generic function Math.Abs() but i don't know how to make this. if i create a class Math i can't create method Abs for T value because i don't know the type of T.
If someone know how to do this ?
Thanks
Six options:
T
Dictionary<Type, Func<object, object>>
containing a delegate for every type you care aboutUse dynamic
in .NET 4:
public T Foo<T>(T value)
{
dynamic d = value;
return Math.Abs(d);
}
Use something like Marc Gravell's generic operators part of MiscUtil
I'd probably go for the overload option if possible. It will constrain you appropriately at compile-time, and avoid any potentially time-consuming boxing/reflection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With