我正在編寫通用二進制搜索樹。我需要比較兩種泛型類型。如何做到這一點,假設用戶在T類中實現了IComparable
。比較通用類型
private void Insert(T newData, ref Node<T> currentRoot)
{
if (currentRoot == null)
{
currentRoot = new Node<T>(newData);
return;
}
if (newData <= currentRoot.data) //doesn't work, need equivalent functionality
Insert(newData, ref currentRoot.lChild);
else
Insert(newData, ref currentRoot.rChild);
}
不'newData.CompareTo(currentRoot.data)'工作? –
只能用'where T:IComparable'作爲@BrokenGlass指出的 –
devnull