[英]generic class with constraint IEqualityComparer
我對C#中的泛型類很陌生。 我試圖創建一個並遇到編譯器錯誤,但我不確定該如何解決。
基本上,我有一個實現ICollection的類G
public class G<T> : ICollection<T> where T : IEqualityComparer
{
private ArrayList _members = new ArrayList();
public void Add(T item)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(T item)
{
throw new NotImplementedException();
}
public void CopyTo(T[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public int Count
{
get { throw new NotImplementedException(); }
}
public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}
public bool Remove(T item)
{
throw new NotImplementedException();
}
public IEnumerator<T> GetEnumerator()
{
foreach (var item in _members)
{
yield return (T)item;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
我希望能夠在G中進行比較,並能夠在G中進行查找,因此我提出了一個約束,即T必須實現IEqualityComparer。 然后,我有一個稱為IntegerClass的實際類,它實現了IEqualityComparer,如下所示。 到目前為止,一切都很好,沒有編譯器錯誤。
public class IntegerClass : IEqualityComparer<int>
{
public bool Equals(int x, int y)
{
throw new NotImplementedException();
}
public int GetHashCode(int obj)
{
throw new NotImplementedException();
}
}
但是,當我嘗試在上面創建G的實例時。 我遇到了編譯器錯誤。
class Program
{
static void Main(string[] args)
{
G<IntegerClass> i = new G<IntegerClass>();
}
}
錯誤是:
The type 'TestGeneric.IntegerClass' cannot be used as type parameter 'T' in the generic type or method 'TestGeneric.G<T>'.
There is no implicit reference conversion from 'TestGeneric.IntegerClass' to 'System.Collections.IEqualityComparer'
有人可以指出我忽略的地方嗎? 為什么我需要轉換? 我所做的只是用實現IEqualityComparer接口的IntegerClass替換了類T。 否則我該怎么辦? 我對這種通用知識並不陌生,但是發現它非常有用。 我認為如果我正確理解它可能會非常有用。 請幫忙。
更新:根據一些建議,我發現了問題所在,並按如下所示更新了代碼:
public class IntegerClass : IEqualityComparer
{
public bool Equals(object x, object y)
{
throw new NotImplementedException();
}
public int GetHashCode(object obj)
{
throw new NotImplementedException();
}
}
public class G<T> : ICollection<T> where T : IEqualityComparer
{
private ArrayList _members = new ArrayList();
public void Add(T item)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(T item)
{
throw new NotImplementedException();
}
public void CopyTo(T[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public int Count
{
get { throw new NotImplementedException(); }
}
public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}
public bool Remove(T item)
{
throw new NotImplementedException();
}
public IEnumerator<T> GetEnumerator()
{
foreach (var item in _members)
{
yield return (T)item;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
我認為這可能會奏效,但我收到以下警告:
'TestGeneric.IntegerClass.Equals(object,object)'隱藏繼承的成員'object.Equals(object,object)'。 如果要隱藏,請使用new關鍵字。
我知道對象具有Equals方法,但是警告沒有意義。 如果不想進行隱藏,是否應該說使用new關鍵字?
您的約束指向非通用的System.Collections.IEqualityComparer
接口,該接口與IEqualityComparer<T>
。
您可以通過在約束中指定通用類型來修復該錯誤。
但是,這不是您想要的。 IEqualityComparer是比較其他內容的類。
您要where T : IEquatable<T>
。
如下更改您的IntegerClass。
public class IntegerClass : IEqualityComparer<IntegerClass>
{
public bool Equals(IntegerClass IC1, IntegerClass IC2)
{
throw new NotImplementedException();
}
public int GetHashCode(IntegerClass obj)
{
throw new NotImplementedException();
}
}
或者您可以將其歸結為對象具有相等函數的事實。 因此,這將工作
public interface IMyComparer
{
object Comparer { get; }
}
public class IntegerClass : IMyComparer, IEqualityComparer<int>
{
public object Comparer { get { return this; } }
public bool Equals(int x, int y)
{
throw new NotImplementedException();
}
public int GetHashCode(int obj)
{
throw new NotImplementedException();
}
}
public class G<T> : ICollection<T> where T : IMyComparer
{
Your implementations
}
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.