• <tfoot id='WGANC'></tfoot>

      <bdo id='WGANC'></bdo><ul id='WGANC'></ul>

        <legend id='WGANC'><style id='WGANC'><dir id='WGANC'><q id='WGANC'></q></dir></style></legend>
        <i id='WGANC'><tr id='WGANC'><dt id='WGANC'><q id='WGANC'><span id='WGANC'><b id='WGANC'><form id='WGANC'><ins id='WGANC'></ins><ul id='WGANC'></ul><sub id='WGANC'></sub></form><legend id='WGANC'></legend><bdo id='WGANC'><pre id='WGANC'><center id='WGANC'></center></pre></bdo></b><th id='WGANC'></th></span></q></dt></tr></i><div id='WGANC'><tfoot id='WGANC'></tfoot><dl id='WGANC'><fieldset id='WGANC'></fieldset></dl></div>

        <small id='WGANC'></small><noframes id='WGANC'>

      1. 在 IEqualityComparer 中包装委托

        Wrap a delegate in an IEqualityComparer(在 IEqualityComparer 中包装委托)
        <i id='38HXD'><tr id='38HXD'><dt id='38HXD'><q id='38HXD'><span id='38HXD'><b id='38HXD'><form id='38HXD'><ins id='38HXD'></ins><ul id='38HXD'></ul><sub id='38HXD'></sub></form><legend id='38HXD'></legend><bdo id='38HXD'><pre id='38HXD'><center id='38HXD'></center></pre></bdo></b><th id='38HXD'></th></span></q></dt></tr></i><div id='38HXD'><tfoot id='38HXD'></tfoot><dl id='38HXD'><fieldset id='38HXD'></fieldset></dl></div>

              <tbody id='38HXD'></tbody>
          • <legend id='38HXD'><style id='38HXD'><dir id='38HXD'><q id='38HXD'></q></dir></style></legend>
              <tfoot id='38HXD'></tfoot>

              • <bdo id='38HXD'></bdo><ul id='38HXD'></ul>
              • <small id='38HXD'></small><noframes id='38HXD'>

                1. 本文介绍了在 IEqualityComparer 中包装委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  几个 Linq.Enumerable 函数采用 IEqualityComparer<T>.是否有一个方便的包装类来适应 delegate(T,T)=>bool 来实现 IEqualityComparer<T>?编写一个很容易(如果您忽略了定义正确哈希码的问题),但我想知道是否有开箱即用的解决方案.

                  Several Linq.Enumerable functions take an IEqualityComparer<T>. Is there a convenient wrapper class that adapts a delegate(T,T)=>bool to implement IEqualityComparer<T>? It's easy enough to write one (if your ignore problems with defining a correct hashcode), but I'd like to know if there is an out-of-the-box solution.

                  具体来说,我想对 Dictionary 进行设置操作,只使用 Keys 来定义成员资格(同时根据不同的规则保留值).

                  Specifically, I want to do set operations on Dictionarys, using only the Keys to define membership (while retaining the values according to different rules).

                  推荐答案

                  通常情况下,我会通过在答案上评论 @Sam 来解决这个问题(我已经对原始帖子进行了一些编辑,以便在没有改变行为.)

                  Ordinarily, I'd get this resolved by commenting @Sam on the answer (I've done some editing on the original post to clean it up a bit without altering the behavior.)

                  以下是我的 @Sam 的回答的即兴演奏, 对默认散列策略进行了 [IMNSHO] 关键修复:-

                  The following is my riff of @Sam's answer, with a [IMNSHO] critical fix to the default hashing policy:-

                  class FuncEqualityComparer<T> : IEqualityComparer<T>
                  {
                      readonly Func<T, T, bool> _comparer;
                      readonly Func<T, int> _hash;
                  
                      public FuncEqualityComparer( Func<T, T, bool> comparer )
                          : this( comparer, t => 0 ) // NB Cannot assume anything about how e.g., t.GetHashCode() interacts with the comparer's behavior
                      {
                      }
                  
                      public FuncEqualityComparer( Func<T, T, bool> comparer, Func<T, int> hash )
                      {
                          _comparer = comparer;
                          _hash = hash;
                      }
                  
                      public bool Equals( T x, T y )
                      {
                          return _comparer( x, y );
                      }
                  
                      public int GetHashCode( T obj )
                      {
                          return _hash( obj );
                      }
                  }
                  

                  这篇关于在 IEqualityComparer 中包装委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                  Func Delegate vs Function(函数委托与函数)
                  • <i id='jKrgD'><tr id='jKrgD'><dt id='jKrgD'><q id='jKrgD'><span id='jKrgD'><b id='jKrgD'><form id='jKrgD'><ins id='jKrgD'></ins><ul id='jKrgD'></ul><sub id='jKrgD'></sub></form><legend id='jKrgD'></legend><bdo id='jKrgD'><pre id='jKrgD'><center id='jKrgD'></center></pre></bdo></b><th id='jKrgD'></th></span></q></dt></tr></i><div id='jKrgD'><tfoot id='jKrgD'></tfoot><dl id='jKrgD'><fieldset id='jKrgD'></fieldset></dl></div>

                          <tbody id='jKrgD'></tbody>

                            <bdo id='jKrgD'></bdo><ul id='jKrgD'></ul>
                            <tfoot id='jKrgD'></tfoot>
                            <legend id='jKrgD'><style id='jKrgD'><dir id='jKrgD'><q id='jKrgD'></q></dir></style></legend>

                            <small id='jKrgD'></small><noframes id='jKrgD'>