首页 > 解决方案 > 结构类型的扩展方法

问题描述

我有这两个重复的方法作为扩展方法,但我想把它们浓缩成一个扩展方法,

我曾想过使用约束: where T : Struct ,

但是那么 bool ,其他结构类型呢,有没有办法只限制 int decimal float long 等。

   public static bool IsBetween(this double value, double lowerBound, double upperBound)
    {
        return value >= lowerBound && value <= upperBound;
    }


    public static bool IsBetween(this decimal value, decimal lowerBound, decimal upperBound)
    {
        return value >= lowerBound && value <= upperBound;
    }

标签: c#extension-methods

解决方案


推荐阅读