标签: icomparable

比较实现IComparable的项目的问题

我正在研究一种扩展方法,它通过特定的选择器找到最小项目.代码下方

    public static T MinBy<T, K>(this IEnumerable<T> src, Func<T, K> selector) where K : struct, IComparable, IConvertible
    {
        var min = default(K);
        T minItem = default(T);
        foreach (var item in src)
        {
            var current = selector(item);
            if (current < min)
            {
                min = current;
                minItem = item;
            }
        }

        return minItem;

    }
Run Code Online (Sandbox Code Playgroud)

它给出了错误Error Operator '<' cannot be applied to operands of type 'K' and 'K'.但我已经指定了通用约束K应该是Struct and IComparable.我相信所有数字数据类型都可以满足于此.

那为什么这是一个无效的操作.

c# generics icomparable

5
推荐指数
2
解决办法
2768
查看次数

在实现IComparable <T>时假设这个!= null

我有一个T实现类型的对象IComparable<T>.实施时bool Equals (T obj)可以省略支票if (ReferenceEquals(this, null)) { DoSomething() }吗?我可以假设,因为可以调用函数this已经不是空的吗?

非常感谢你.

c# icomparable

5
推荐指数
1
解决办法
180
查看次数

如何使用"is"来测试类型是否支持IComparable?

我想在排序之前检查一个类型是否支持IComparable,但是我发现检查一个类型是否支持使用"is"的IComparable接口并不总是给我正确的答案.例如,typeof(int) is IComparable返回false,即使int支持IComparable接口.

我注意到typeof(int).GetInterfaces()列出IComparable并typeof(int).GetInterface("IComparable")返回IComparable类型,那么为什么"is"不能像我预期的那样工作呢?

.net c# reflection icomparable

5
推荐指数
2
解决办法
1458
查看次数

通用约束 - 我不确定如何用这两种情况来解决这种情况

基本上我有以下几点:

public static bool IsBetween<T>(this T value, T a, T b)
    where T : IComparable
{
    ...
}

public static bool IsBetween<T>(this T value, T a, T b)
    where T : IComparable<T>
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

问题是我不能这样做,因为你不能拥有一个具有相同签名的成员,即使约束不同.但是,没有办法说明约束是IComparableOR IComparable<T>.所以,我不确定除了选择一个并继续使用它之外该做什么.并且,无论我选择哪一个,我都会失败,因为它们是分开的,不会相互继承(这是有道理的).

我在这里遗漏了一些东西是否有办法完成两者的使用,或者我将不得不选择一个(可能是通用版本)?

c# generics extension-methods icomparable generic-constraints

5
推荐指数
1
解决办法
196
查看次数

C#泛型类和EqualityComparer

任何人都可以在下面的类声明中解释我的错误:

private class PriorityQueueEntry<TPriorityValue,IIdentifiableEntry,IType> :
        IComparer<PriorityQueueEntry<TPriorityValue,IIdentifiableEntry,IType>>
            where TPriorityValue : IComparable
            where IIdentifiableEntry : Identifier<IType>
    {
        public TPriorityValue Priority{get;private set;}
        public IIdentifiableEntry Entry{get;private set;}

        public PriorityQueueEntry(TPriorityValue val,IIdentifiableEntry entry)
        {
            Priority = val;
            Entry = entry;
        }
        public int Compare(PriorityQueueEntry<TPriorityValue,IIdentifiableEntry,IType> first, PriorityQueueEntry<TPriorityValue,IIdentifiableEntry,IType> second) 
        {
            if (first.Priority.CompareTo(second.Priority) < 0)
            {
                return -1;
            }
            else if (first.Priority.CompareTo(second.Priority) > 0)
            {
                return 1;
            }
            return EqualityComparer<IIdentifiableEntry>.Default.Equals( first.Entry.Id, second.Entry.Id);
        }
    }
Run Code Online (Sandbox Code Playgroud)

编译器在我使用EqualityComparer的行上抱怨.错误如下:

错误CS0176:无法使用实例引用访问静态成员`object.Equals(object,object)',而是使用类型名称限定它

我无法看到我在哪里使用实例引用.


对不起,我的错.我发布了一个不完整的问题 为了完整起见,Idetifier类只是以下内容:

public interface Identifier<ID_TYPE> 
{
    ID_TYPE Id{get;set;}
}
Run Code Online (Sandbox Code Playgroud)

在那里使用EqualityComparer,是由于复制和粘贴错误(对不起家伙,今天太多的通用代码).

当然我的问题被误解了,因为我没有给你所需要回答的所有元素(我很快就会把它删除).我需要IType …

c# generics static icomparable icomparer

5
推荐指数
2
解决办法
1822
查看次数

如何将一个int数组发送到一个接受IComparable数组的函数?

我有一个接受数组的C#函数 IComparable

public static void sort(IComparable[] a){//...}
Run Code Online (Sandbox Code Playgroud)

如果我向这个函数发送一个字符串数组,它被接受,但即使结构Int32扩展,也不接受一个int数组IComparable.

public struct Int32 : IComparable, IFormattable, 
IConvertible, IComparable<int>, IEquatable<int>
Run Code Online (Sandbox Code Playgroud)

第一个问题是为什么不可能向这样的函数发送值类型的数组.

第二个问题是我应该如何将值类型的数组发送到接受数组的函数IComparable.

c# icomparable value-type reference-type

5
推荐指数
1
解决办法
380
查看次数

为什么我在C#中的List.Sort方法颠倒了我的列表顺序?

我有一个通用列表中的项目列表:

  • A1(排序索引1)
  • A2(排序索引2)
  • B1(排序索引3)
  • B2(排序索引3)
  • B3(排序索引3)

它们上的比较器采用以下形式:

this.sortIndex.CompareTo(other.sortIndex)
Run Code Online (Sandbox Code Playgroud)

当我在项目列表上执行List.Sort()时,我得到以下顺序:

  • A1
  • A2
  • B3
  • B2
  • B1

它显然起作用,排序索引的顺序是正确的,但我真的不希望它重新排序'B'项.

我可以对比较器进行调整以解决这个问题吗?

.net c# sorting icomparable

4
推荐指数
2
解决办法
1738
查看次数

本地C#支持检查IEnumerable是否已排序?

是否有任何LINQ支持检查是否IEnumerable<T>已排序?我有一个我想要验证的枚举按非降序排序,但我似乎无法在C#中找到它的本机支持.

我使用IComparables<T>以下方法编写了自己的扩展方法:

public static bool IsSorted<T>(this IEnumerable<T> collection) where T : IComparable<T>
{
   Contract.Requires(collection != null);

   using (var enumerator = collection.GetEnumerator())
   {
      if (enumerator.MoveNext())
      {
         var previous = enumerator.Current;

         while (enumerator.MoveNext())
         {
            var current = enumerator.Current;

            if (previous.CompareTo(current) > 0)
               return false;

            previous = current;
         }
      }
   }

   return true;
}
Run Code Online (Sandbox Code Playgroud)

一个使用IComparer<T>对象:

public static bool IsSorted<T>(this IEnumerable<T> collection, IComparer<T> comparer)
{
   Contract.Requires(collection != null);

   using (var enumerator = collection.GetEnumerator())
   {
      if (enumerator.MoveNext())
      {
          var …
Run Code Online (Sandbox Code Playgroud)

c# linq sorted icomparable sortedlist

4
推荐指数
1
解决办法
1896
查看次数

IComparable 是在字典中强制执行唯一键的最佳方法吗?

我有一个类 MyClass,我想将它作为字典的键,如下所示:

Dictionary<MyClass, string> dict = new Dictionary<MyClass, string>();
Run Code Online (Sandbox Code Playgroud)

我想确保 MyClass 是唯一的键,并且通过查看 来指定唯一性MyClass.UniqueProperty。我的第一个想法是重载该==运算符,但我发现 C# 不允许这样做。然后我找到了IComparable接口。这能起到作用吗?或者我应该重载 Object.Equals(obj) 吗?

c# dictionary key icomparable primary-key

4
推荐指数
1
解决办法
3023
查看次数

为什么我可以将 Int 转换为 IComparable?

我在一个测验中遇到了一个问题,即“下面在做什么?”:

// Value is an int
IComparable thing = (IComparable)value;
Run Code Online (Sandbox Code Playgroud)

显然答案是拳击,但我不知道为什么。为什么这被认为是拳击,它在做什么?我的印象是拳击只能用object.

c# boxing icomparable

4
推荐指数
1
解决办法
269
查看次数