简体   繁体   English

C#函数中的多种数据类型输入

[英]Multiple data type input in C# function

I am switching from an Arduino (c language) to a Netduino (c# language). 我正在从Arduino(c语言)切换到Netduino(c#语言)。

In my arduino program I have the following function (built-in): 在我的arduino程序中,我具有以下功能(内置):

Constrain 约束条件

I would like to convert this to C#. 我想将其转换为C#。 I have the following code: 我有以下代码:

int ConstrainValue(int value, int min, int max)
    {
        int Value = value;
        int Min = min;
        int Max = max;

        if (Value >= Max)
        {
            Value = Max;
            return Value;
        }
        else if (Value <= Max)
        {
            Value = Min;
            return Value;
        }
        return Value;
    }

However, I would also like to be able to use this for the double datatype. 但是,我也希望能够将其用于double数据类型。 Is it possible to modify the function so multiple datatypes can be used? 是否可以修改函数以便可以使用多种数据类型?

It is, using IComparable . 使用IComparable

static T ConstrainValue<T>(T value, T min, T max) where T : IComparable
{
    if (value.CompareTo(max) > 0)
        return max;
    else if (value.CompareTo(min) < 0)
        return min;
    return value;
}

Yes it is, you need to make it a generic function, something like that: 是的,您需要将其设为通用函数,如下所示:

T ConstrainValue<T>(T value, T min, T max) where T : IComparable

I think you'll need to add some more interfaces though 我认为您需要添加更多接口

By specifying struct , you will not get boxing when calling the method, but by using IComparable you will still get it when calling CompareTo because that interface method takes an object . 通过指定struct ,在调用该方法时不会得到装箱,但是通过使用IComparable ,在调用CompareTo时仍会得到装箱,因为该接口方法带有object

So use IComparable<T> and I'm pretty sure there's no boxing now: 因此,使用IComparable<T> ,我很确定现在没有拳击了:

    private static T ConstrainValue<T>(T value, T min, T max)
      where T : struct, IComparable<T>
    {
        if (value.CompareTo(max) > 0)
        {
            return max;
        }

        if (value.CompareTo(min) < 0)
        {
            return min;
        }

        return value;
    }

By using generics you can use multiple datatypes 通过使用泛型,您可以使用多种数据类型

T ConstrainValue(T value, T min, T max) where T : IComparable T ConstrainValue(T值,T min,T max)其中T:IComparable

.net micro (netduino) does NOT support generics as of v4.2. 从v4.2开始,.net micro(netduino)不支持泛型

You would have to use another scheme such as a function that takes objects as arguments and then does the work. 您将不得不使用另一种方案,例如将对象作为参数然后执行工作的函数。 You'll then have to use 'as' or casting on the return in the calling function: 然后,您必须在调用函数的返回值上使用“ as”或强制转换:

    object ConstrainValueInt(object value, object min, object max)
        {
   /* this could still get you in trouble with bool type */
          if (value.GetType().isValueType && min.GetType().isValueType && max.GetType().isValueType )
          return ( (value >= max)? max : ((value <= min)? min : value));
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM