C# 如何在C中的两个短数字之间找到另一个短数字#

C# 如何在C中的两个短数字之间找到另一个短数字#,c#,C#,我有两个短号码20101、20141如果我通过了20121和20131,那么它应该返回在范围内可用的号码。如果我通过20081和20091,那么它应该返回false,与20142到20154相同 如何在两个短数字之间找到 bool TestRange (int numberToCheck, int bottom, int top) { return (numberToCheck >= bottom && numberToCheck <= top); } bool

我有两个短号码20101、20141如果我通过了20121和20131,那么它应该返回在范围内可用的号码。如果我通过20081和20091,那么它应该返回false,与20142到20154相同

如何在两个短数字之间找到

bool TestRange (int numberToCheck, int bottom, int top)
{
  return (numberToCheck >= bottom && numberToCheck <= top);
}
bool测试范围(整数检查、整数底部、整数顶部)
{

返回(numberToCheck>=bottom&&numberToCheck将签名更改为
booltestrange(int[]numbersToCheck,int-bottom,int-top)
并迭代
numbersToCheck
并根据需要返回

由于我需要经常使用类似的方法,而且我很懒,所以我编写了一个扩展方法,以检查两个值之间是否有任何整数

public static class IntExtensions
{
    public static bool Between(this int value, int lowerBound, int upperBound)
    {
        return value >= lowerBound && value <= upperBound;
    }
}
或者只返回与约束匹配的数字作为新数组:

return Array.Where(x => x.Between(integer, integer)).ToArray();
一般情况下,如果要测试多个(任意数量)项目,我建议修改签名和Linq:

//如果要传递许多项,请传递IEnumerable
布尔测试范围(IEnumerable numbersToCheck、int-bottom、int-top){
if(null==numberToCheck)
抛出新ArgumentNullException(“numbersToCheck”);//或返回true或false。。。

返回numbersToCheck.All(item=>item>=bottom&&top井..只需调用它两次!?
return Array.Where(x => x.Between(integer, integer)).ToArray();
// If you want to pass many items, pass IEnumerable<T> 
bool TestRange(IEnumerable<int> numbersToCheck, int bottom, int top) {
  if (null == numberToCheck)
    throw new ArgumentNullException("numbersToCheck"); // or return true or false... 

  return numbersToCheck.All(item => item >= bottom && top <= item);
}

...

if (TestRange(new int[] {20121, 20131}, 20101, 20141)) {...}