允许一种方法只采取数字?

时间:2016-08-17 21:45:57

标签: c# methods numbers

已更新:如何使用两个参数来创建一个方法来采用任何原始数字类型,例如intuint,{{ 1}},float等,(double除外)?

我目前正在使用bool,但这意味着该方法可以接受任何类型。

object

4 个答案:

答案 0 :(得分:3)

好吧,因为你想使用除bool之外的所有原始类型,那怎么样?

public int MyNumberMethod<T>(T number) where T : struct
{
    if (!(number is bool) && number.GetType().IsPrimitive) 
        return Convert.ToInt32(number);

    return 0;
}

e.g。

MyNumberMethod<short>(5);

答案 1 :(得分:3)

所有数字类型(double和float除外)都可以隐式转换为decimal,float可以隐式转换为double。所以,如果你做了重载,如:

Method(double, double)
Method(decimal, double)
Method(decimal,decimal)
Method(double, decimal)

您的方法可以使用任意两个数字调用,但只能使用任意两个数字。

答案 2 :(得分:3)

这可能不如其他答案那么好,但另一种选择是创建您自己的结构,其中您只允许某些数据类型的值:

chdir()

每次更改值或创建结构的新实例时,它将验证输入值的数据类型是否与public struct Number { #region Static methods and fields private static readonly Type[] allowedTypes = new Type[] { typeof(int), typeof(uint), typeof(short), typeof(ushort), typeof(long), typeof(ulong), typeof(byte), typeof(sbyte), typeof(float), typeof(double), typeof(decimal) }; private static void CheckIsNumber(dynamic val) { if (Array.IndexOf(allowedTypes, val.GetType()) == -1) { throw new InvalidCastException("Input type must be a number."); } } #endregion #region Constructor public Number(dynamic Value) { Number.CheckIsNumber(Value); _value = Value; } #endregion #region Properties private dynamic _value; public dynamic Value { get { return _value; } set { Number.CheckIsNumber(value); _value = value; } } #endregion #region Overridden methods public override bool Equals(object obj) { return _value.Equals(obj); } public override int GetHashCode() { return _value.GetHashCode(); } public override string ToString() { return _value.ToString(); } #endregion #region Conversion operators - Number public static implicit operator Number(uint val) { return new Number(val); } public static implicit operator Number(short val) { return new Number(val); } public static implicit operator Number(ushort val) { return new Number(val); } public static implicit operator Number(long val) { return new Number(val); } public static implicit operator Number(ulong val) { return new Number(val); } public static implicit operator Number(byte val) { return new Number(val); } public static implicit operator Number(float val) { return new Number(val); } public static implicit operator Number(double val) { return new Number(val); } public static implicit operator Number(decimal val) { return new Number(val); } #endregion #region Conversion operators - Misc. data types public static implicit operator int(Number num) { return (int)num.Value; } public static implicit operator uint(Number num) { return (uint)num.Value; } public static implicit operator short(Number num) { return (short)num.Value; } public static implicit operator ushort(Number num) { return (ushort)num.Value; } public static implicit operator long(Number num) { return (long)num.Value; } public static implicit operator ulong(Number num) { return (ulong)num.Value; } public static implicit operator byte(Number num) { return (byte)num.Value; } public static implicit operator sbyte(Number num) { return (sbyte)num.Value; } public static implicit operator float(Number num) { return (float)num.Value; } public static implicit operator double(Number num) { return (double)num.Value; } public static implicit operator decimal(Number num) { return (decimal)num.Value; } #endregion } 数组中的任何项匹配。如果不是,它将抛出allowedTypes

我还添加了转换运算符,可以将它作为普通数字使用,因此您可以像使用任何其他数值数据类型一样使用它:

InvalidCastException

但是请记住,在使用Number myNum = 3.5; myNum += 10.4; double something = myNum - 6.0; double等时必须添加小数点,否则它会假设数字是整数:

float

所有这些都说明了,您将如何将它用于您的方法:

Number myNum = 3.5;
myNum -= 2;
MessageBox.Show(myNum.ToString()); //Shows "1" as the second line converts 'myNum' into an integer.

感谢转化运算符,您不需要指定public int[] MyNumberMethod(Number a, Number b) { try { return new int[] { Convert.ToInt32(b), Convert.ToInt32(a) }; } catch(InvalidCastException) { return new int[] { 0, 0 }; } } 转换。例如:

(Number)

答案 3 :(得分:0)

首先,您必须调用其他函数,如GenericNumberMethod,它将包含对MyNumberMethod的调用。对于每种数据类型,您必须具有GenericNumberMethod的实现,并具有相应的参数

public int MyNumberMethod(object number) {
    return Convert.ToInt32(number);
}

public int GenericNumberMethod(int number) {
    return MyNumberMethod(number);
}

public int GenericNumberMethod(decimal number) {
    return MyNumberMethod(number);
}