错误 CS0411 无法从用法中推断方法 'Program.Binary_Search(T[], T, IComparer)' 的类型参数

Error CS0411 The type arguments for method 'Program.Binary_Search(T[], T, IComparer)' cannot be inferred from the usage

我不明白我在这里做错了什么。

我的二进制搜索方法收到此错误消息。

Error CS0411 The type arguments for method 'Program.Binary_Search(T[], T, IComparer)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

      //Binary search method.
    public static void BinarySearch<T>(T[] data)
    {
        Binary_Search(data, Console.ReadLine(), Comparer<T>.Default);
    }

        //Binary search algorithm
        public static int Binary_Search<T>(T[] data, T searchFor, IComparer<T>comparer)
        {
            int high, low, mid;
            high = data.Length - 1;
            low = 0;
            //if the first element of the array is what I'm looking for then return that element.
            if (data[0].Equals(searchFor))
                return 0;
            //else if highest element in the array is the item im looking for then return the element.
            else if (data[high].Equals(searchFor))
                return high;
            else
            {
                //While low point is lower than or equal with high point set the mid point to be in the middle of the highest and lowers point.
                while (low <= high)
                {
                    mid = (high + low) / 2;
                    //Compare mid point to the item searched, if the difference is 0 it means the item is there, return the element.
                    if (comparer.Compare(data[mid], searchFor) == 0)
                        return mid;
                    //Else if the difference between mid point and searched item is bigger than 0, the searched item must be lower than mid point,
                    //set the new high point to be current mid -1;
                    else if (comparer.Compare(data[mid], searchFor) > 0)
                        high = mid - 1;
                    //Else (Current mid point is lower than the searched item) set the new low point to be the current mid point +1.
                    else
                        low = mid + 1;
                }
                return -1;
            }
        }

主程序

  static void Main(string[] args)
            {
                Console.WriteLine("Analysis of Seismic Data.\n");
                //Read in the files and put them into arrays.
                String[] Years = File.ReadAllLines(@"Data\Year_1.txt");
                String[] Months = File.ReadAllLines(@"Data\Month_1.txt");
                String[] Days = File.ReadAllLines(@"Data\Day_1.txt");
                String[] Times = File.ReadAllLines(@"Data\Time_1.txt");
                String[] Depths = File.ReadAllLines(@"Data\Depth_1.txt");
                String[] Latitudes = File.ReadAllLines(@"Data\Latitude_1.txt");
                String[] Longitudes = File.ReadAllLines(@"Data\Longitude_1.txt");
                String[] Magnitudes = File.ReadAllLines(@"Data\Magnitude_1.txt");
                String[] Regions = File.ReadAllLines(@"Data\Region_1.txt");
                String[] IRIS_IDs = File.ReadAllLines(@"Data\IRIS_ID_1.txt");
                String[] Timestamps = File.ReadAllLines(@"Data\Timestamp_1.txt");
        //Read in user's decision.
        Console.Write("Select which Collection is to be analysed: ");

        int collectionDecision = Convert.ToInt32(Console.ReadLine());

        //Selected which collection is to be analyzed
        switch (collectionDecision)
        {
            case 1:
                //Create another switch statement to select either ascending or descending sort.
                Console.WriteLine("\nWould you like to sort the Collection in Ascending or Descending order?");
                Console.WriteLine("1-Ascending");
                Console.WriteLine("2-Descending");
                int sortingDecision = Convert.ToInt32(Console.ReadLine());
                switch (sortingDecision)
                {
                    case 1:
                        //Using the default QuickSort option to sort the collection in an ascending order.
                        QuickSort(Years);
                        Console.WriteLine("Contents of the Ascending Year Collection: ");
                        foreach (var year in Years)
                        {
                            Console.WriteLine(year);
                        }
                        Console.WriteLine("\nEnter the Number/Name of the items you are looking for from the previously selected Collection.");
                        Console.Write("Search: ");
                        //How do I implement Binary Search here to search for specific items from a selected Array/Collection and display them?

                        break;
                    case 2:
                        //Using Comparer<T>.Create to create a custom object and change the algorithm to sort in a Descending order.
                        QuickSort(Years, Comparer<string>.Create((a, b) => b.CompareTo(a)));
                        Console.WriteLine("Contents of the Descending Year Collection: ");
                        foreach (var year in Years)
                        {
                            Console.WriteLine(year);
                        }
                        Console.WriteLine("\nEnter the Number/Name of the items you are looking for from the previously selected Collection.");
                        Console.Write("Search: ");
                        //How do I implement Binary Search here to search for specific items from a selected Array/Collection and display them?
                        break;

                }
                break;

我正在尝试制作一个应用程序,用户可以在其中选择他们要搜索的数组,一旦选择了数组,用户就会对数组进行排序,然后用户可以选择使用以下命令从数组中搜索特定项目二进制搜索。例如,如果用户在 Years 数组中,他们可以输入“2016”,二分搜索将从列表中搜索出所有 2016 年的项目并将它们显示在控制台上。字符串数组包含不同的数据类型:整数、字符串、双精度数。

这是一个简单的示例,说明您的 Binary_Search 方法如何处理字符串:

public static void Main(string[] args)
{
    Console.WriteLine("Please enter a value to search:");

    var toSearch = Console.ReadLine();

    var strings = GetOrderedArrayOfStrings(); // assuming here we have an ordered string[]

    var position = Binary_Search(strings, toSearch, Comparer<string>.Default);

    if(position == -1)
        Console.WriteLine("Not found");
    else
        Console.WriteLine($"Found at position {position}");
}

您看到的错误消息是因为编译器需要在您的方法中使用 T 的实例(而不是 string,它与您声明的泛型类型不同)。