मुझे लगता है कि आप एक प्रकार का शब्दकोश विस्तार कर रहे थे जो डुप्लिकेट कुंजी की अनुमति नहीं देता है।
इस कार्यान्वयन के बारे में क्या। मुझे लगता है कि आपकी वस्तु IComparable लागू करता है।
class BaseSortedCollection<T> : Collection<T>, ICollection<T>, IEnumerable<T>,
System.Collections.ICollection, System.Collections.IEnumerable
where T : IComparable<T>
{
/// <summary>
/// Adds an item to the Collection<T> at the correct position.
/// </summary>
/// <param name="item">The object to add to </param>
public new void Add(T item)
{
int pos = GetInsertPositio(item);
base.InsertItem(pos, item);
}
/// <summary>
/// Convinience function to add variable number of items in one Functioncall
/// </summary>
/// <param name="itemsToBeAdded">The items to be added.</param>
/// <returns>this to allow fluent interface</returns>
public AutoSortCollection<T> AddItems(params T[] itemsToBeAdded)
{
foreach (var item in itemsToBeAdded)
Add(item);
return this;
}
/// <summary>
/// Get position where item should be inserted.
/// </summary>
/// <param name="item"></param>
/// <returns>Get position where item should be inserted.</returns>
private int GetInsertPositio(T item)
{
if (item == null)
throw new ArgumentNullException();
for (int pos = this.Count - 1; pos >= 0; pos--)
{
if (item.CompareTo(this.Items[pos]) > 0)
return pos + 1;
}
return 0;
}
}
इस (MSTest का उपयोग कर)
/// <summary>
///A test sorting for SCCPackageEx Constructor
///</summary>
[TestMethod()]
public void SortingTest()
{
BaseSortedCollection<int> collection = new BaseSortedCollection<int>().AddItems(1,5,3,2,4,0);
Assert.AreEqual(6, collection.Count, "collection.Count");
for(int i=0; i <=5; i++)
Assert.AreEqual(i, collection[i], "collection[" + i + "]");
}
काम करना चाहिए मैं यह मुश्किल अपने BaseSortedCollection के कुछ कार्यान्वयन विवरण जानने के बिना अच्छी सलाह देने के लिए लगता है। डुप्लिकेट पहली जगह क्यों काम नहीं कर रहे हैं? क्यों नहीं (शायद संतुलित) बाइनरी पेड़ का उपयोग अपने सामान के लिए डेटा संरचना के रूप में नहीं करते? – Lucero
आपकी प्रतिक्रिया के लिए धन्यवाद। डुप्लिकेट काम नहीं कर रहे हैं क्योंकि दो वस्तुओं में एक ही आइटम आईडी है, जो कि संग्रह में कुंजी के रूप में उपयोग किया जा रहा है। –