Answer the question
In order to leave comments, you need to log in
Is there a C# analogue of numeric_limits from C++?
Actually, subject. For example, in my C++ template function, I could write something like
template<typename T> void fun()
{
auto x = numeric_limits<T>::max();
}
Answer the question
In order to leave comments, you need to log in
Numeric types in .Net have static fields MinValue and MaxValue, so your task can be solved by reflection:
public class MyNumeric<T> where T : struct, IComparable<T>, IEquatable<T>, IConvertible
{
public static readonly T MaxValue = ReadStaticField("MaxValue");
public static readonly T MinValue = ReadStaticField("MinValue");
private static T ReadStaticField(string name)
{
FieldInfo field = typeof(T).GetField(name, BindingFlags.Public | BindingFlags.Static);
if (field == null)
{
throw new InvalidOperationException("Нечисловой тип: " + typeof(T).Name);
}
return (T)field.GetValue(null);
}
}
Console.WriteLine(MyNumeric<int>.MaxValue);
Console.WriteLine(MyNumeric<float>.MinValue);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question