如何在执行Array.BinarySearch时调用IComparable

时间:2022-08-27 20:47:21

The MSDN page documenting the behavior of the BinarySearch method shows that both the array and the value being searched may implement IComparable:

记录BinarySearch方法行为的MSDN页面显示数组和被搜索的值都可以实现IComparable:

1) The page describes

1)页面描述

Either value or every element of array must implement the IComparable interface, which is used for comparisons.

值或数组的每个元素都必须实现IComparable接口,该接口用于比较。

2) Further, the method throws an InvalidOperationException if

2)此外,该方法抛出InvalidOperationException if

value does not implement the IComparable interface, and the search encounters an element that does not implement the IComparable interface.

value不实现IComparable接口,并且搜索遇到不实现IComparable接口的元素。

I was trying to demonstrate this behavior (using the IComparable interface of value) but was unable to. Here is my code:

我试图证明这种行为(使用IComparable接口的值),但无法。这是我的代码:

// Declarations
class Many 
{
    public string data { get; set; }
}
class One : Many, IComparable<Many>
{
    public int CompareTo(Many other)
    {
        Console.WriteLine("Comparator of One invoked");
        if (this.data.Length < other.data.Length) return -1;
        if (this.data.Length > other.data.Length) return 1;
        return 0;
    }
}
...
// action
    Many[] manies = new[] { new Many { data = "1" }, 
                            new Many { data = "22" },  
                            new Many { data = "333" }, 
                            new Many { data = "4444" }, };
    One one = new One {data="333"};
    Console.WriteLine(Array.BinarySearch(manies, one));

When I run this I get a System.InvalidOperationException, which according to the documentation should occur if value does not implement IComparable. However it seems to me that it does implement IComparable.

当我运行它时,我得到一个System.InvalidOperationException,如果value没有实现IComparable,根据文档应该会发生。然而,在我看来它确实实现了IComparable。

How do I get the comparator of value to run, instead of that of the elements in the array?

如何运行值的比较器而不是数组中的元素?

1 个解决方案

#1


2  

Now that I understand the question, change your implementation of One to implement IComparable and not IComparable<Many>. Looking at the documentation I think that is the missing component. You can implement both if you need to, but Array.BinarySearch will not use the IComparable<T> interface.

现在我理解了这个问题,将One的实现改为实现IComparable而不是IComparable 。查看文档,我认为这是缺少的组件。如果需要,您可以实现这两个,但Array.BinarySearch不会使用IComparable 接口。

This code is working for me:

这段代码对我有用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestProject
{
    public class NoCompare
    {
        public string V
        {
            get;
            set;
        }
    }

    public class Compare : IComparable
    {
        public string V
        {
            get;
            set;
        }

        public int CompareTo(object obj)
        {
            NoCompare a = obj as NoCompare;

            if (a == null)
                return -1;

            return String.Compare(V, a.V);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            NoCompare[] strings = new NoCompare[] { new NoCompare() { V = "a" }, new NoCompare() { V = "b" }, new NoCompare() { V = "c" } };

            Compare t = new Compare();
            t.V = "b";

            Array.BinarySearch((object[])strings, t);
       }
    }
}

#1


2  

Now that I understand the question, change your implementation of One to implement IComparable and not IComparable<Many>. Looking at the documentation I think that is the missing component. You can implement both if you need to, but Array.BinarySearch will not use the IComparable<T> interface.

现在我理解了这个问题,将One的实现改为实现IComparable而不是IComparable 。查看文档,我认为这是缺少的组件。如果需要,您可以实现这两个,但Array.BinarySearch不会使用IComparable 接口。

This code is working for me:

这段代码对我有用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestProject
{
    public class NoCompare
    {
        public string V
        {
            get;
            set;
        }
    }

    public class Compare : IComparable
    {
        public string V
        {
            get;
            set;
        }

        public int CompareTo(object obj)
        {
            NoCompare a = obj as NoCompare;

            if (a == null)
                return -1;

            return String.Compare(V, a.V);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            NoCompare[] strings = new NoCompare[] { new NoCompare() { V = "a" }, new NoCompare() { V = "b" }, new NoCompare() { V = "c" } };

            Compare t = new Compare();
            t.V = "b";

            Array.BinarySearch((object[])strings, t);
       }
    }
}