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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Six options:
TDictionary<Type, Func<object, object>>containing a delegate for every type you care aboutUse
dynamicin .NET 4: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.