且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何比较和匹配两种通用类型

更新时间:2022-03-28 21:53:01

简短的答案,在不了解类型的情况下,您无法进行任何形式的排序或高级值检查,因为这毫无意义. (如果有人创建了YourTree< double>或YourTree< Socket>呢?那么寻找前几个字符将毫无意义.)

例外是适用于Object的事物,因此您可以:
-如Kim所说,使用Object.Equals或EqualityComparer通过值相等将它们分组在一起
-使用其哈希码,尽管通常这没有用
-使用其字符串表示形式(ToString()),这可能就是您想要的

听起来您想将T限制为对它们具有特定操作的类,即提供名称,并且可能具有可比性.这意味着您需要一个接口,例如
Short answer, without knowing anything about the type, you can''t do any form of sorting or advanced value checking, because it would make no sense. (What if someone made a YourTree<double>? Or a YourTree<Socket>? Then looking for the first few characters would be meaningless.)

The exceptions are things that work on Object, so you can:
- group them together by value equality using Object.Equals or EqualityComparer as Kim says
- use their hashcode, though this is generally not useful
- use their string representation (ToString()), which might be what you want

It sounds like you want to restrict your T to classes which have particular operations on them, namely, providing a name, and possibly be comparable. That means you need an interface, for example
public interface ITreeNode : IComparable<ITreeNode> {
 string Name { get; }
}



...然后您需要在主类上限制T以支持该接口:



... and then you need to restrict the T on your main class to support that interface:

public class MyBinaryTree<T> where T: ITreeNode {
...
}



现在,您无法创建MyBinaryTree< int> (int不是ITreeNode),因此您需要为要包含在树中的数据类型创建ITreeNode子类.您可以使用一个默认的



Now, you can''t create a MyBinaryTree<int> (int is not an ITreeNode), so you need to create ITreeNode subclasses for the data types you want to be in the tree. You can have a default one that looks like

public class TreeNode<T> : ITreeNode {
 T inner;

 public TreeNode(T inner) { this.inner = inner; }

 public int CompareTo(T other){
  if(inner is IComparable<T>) return ((IComparable<T>)inner).CompareTo(IComparable<T>)other);
  else return StringComparer.CurrentCulture.Compare(inner, other);
 }

 public string Name { get { return inner.Name; } }
}



...,它包装任何类型并使用其比较器(如果有的话),否则使用字符串表示形式. (我认为本机数字类型支持IComparable< T&gt ;,因此它们应该正确排序.)现在您可以做



... which wraps any type and uses its comparer if it has one, otherwise the string representation. (I think the native numeric types support IComparable<T> so they should sort correctly.) Now you can do

var myTree = new MyBinaryTree<TreeNode<int>>();


在MyBinaryTree中,您可以使用ITreeNode的方法(即名称),也可以将节点视为IComparable< T>. (即,将它们传递给排序例程).

希望可以为您提供足够的信息,以便您可以准确地确定如何将其应用于您的情况.

E:很自然,它是当场制作的,未经测试,甚至可能不会编译.此外,泛型类型和HTML不能很好地配合使用.


.. and within MyBinaryTree, you can use the methods of ITreeNode (i.e. the name), and also treat the nodes as IComparable<T> (i.e. pass them to sorting routines).

Hopefully that gives you enough information that you can work out exactly how to apply it to your situation.

E: Naturally, this was made up on the spot, not tested, and probably won''t even compile. Also, generic types and HTML don''t really play well together.


您可以使用 ^ ]调用比较?


You can use Object.Equal[^] to call the compare ?

Or
EqualityComparer<T>.Default.Equals(x, y);