编辑代码

using System;
using System.Collections.Generic;

class Program
{
    static int BinarySearch(List<int> arr, int x)
    {
        int low = 0;
        int high = arr.Count - 1;
        while (low <= high)
        {
            int mid = (low + high) / 2;
            if (arr[mid] == x)
            {
                return mid;
            }
            else if (arr[mid] < x)
            {
                low = mid + 1;
            }
            else
            {
                high = mid - 1;
            }
        }
        return -1;
    }

    static void Main()
    {
        List<int> arr5 = new List<int> { 1, 2, 3, 4, 5 };
        int x = 3;
        int result = BinarySearch(arr5, x);
        Console.WriteLine($"Index of {x} in the array: {result}");

        List<int> arr6 = new List<int> { 1, 2, 3, 4, 5 };
        x = 6;
        result = BinarySearch(arr6, x);
        Console.WriteLine($"Index of {x} in the array: {result}");

        List<int> arr7 = new List<int>();
        x = 3;
        result = BinarySearch(arr7, x);
        Console.WriteLine($"Index of {x} in the array: {result}");

        List<int> arr8 = new List<int> { 1 };
        x = 1;
        result = BinarySearch(arr8, x);
        Console.WriteLine($"Index of {x} in the array: {result}");
    }
}