System.Linq.Enumerable.InRange

Namespace: System.Linq

Class: Enumerable

Method Details

public static bool InRange(int value, int min, int max)

Checks if a value is within a specified range.

Parameters

int value

The value to check.

int min

The lower bound of the range (inclusive).

int max

The upper bound of the range (inclusive).

Returns

bool

true if value is greater than or equal to min and less than or equal to max; otherwise, false.

Remarks

This method determines if a given integer falls within a closed interval defined by a minimum and maximum value.

Examples

Basic Usage
using System;
using System.Linq;

public class Example
{
    public static void Main()
    {
        int number1 = 5;
        int number2 = 15;
        int min = 1;
        int max = 10;

        // Check if number1 is in the range [1, 10]
        bool isInRange1 = Enumerable.InRange(number1, min, max);
        Console.WriteLine($"{number1} is in range [{min}, {max}]: {isInRange1}"); // Output: 5 is in range [1, 10]: True

        // Check if number2 is in the range [1, 10]
        bool isInRange2 = Enumerable.InRange(number2, min, max);
        Console.WriteLine($"{number2} is in range [{min}, {max}]: {isInRange2}"); // Output: 15 is in range [1, 10]: False

        // Edge cases
        bool isMinInRange = Enumerable.InRange(min, min, max);
        Console.WriteLine($"{min} is in range [{min}, {max}]: {isMinInRange}"); // Output: 1 is in range [1, 10]: True

        bool isMaxInRange = Enumerable.InRange(max, min, max);
        Console.WriteLine($"{max} is in range [{min}, {max}]: {isMaxInRange}"); // Output: 10 is in range [1, 10]: True
    }
}