Supplement Insert methods for pair list.

This commit is contained in:
2023-03-03 11:49:32 +08:00
parent da60dc0903
commit 2e54b38d2b
2 changed files with 19 additions and 0 deletions

View File

@@ -4,8 +4,10 @@ using System.Collections.Generic;
namespace Cryville.Common.Collections.Generic { namespace Cryville.Common.Collections.Generic {
public interface IPairList : IList { public interface IPairList : IList {
void Add(object key, object value); void Add(object key, object value);
void Insert(int index, object key, object value);
} }
public interface IPairList<TKey, TValue> : IList<KeyValuePair<TKey, TValue>>, IPairList { public interface IPairList<TKey, TValue> : IList<KeyValuePair<TKey, TValue>>, IPairList {
void Add(TKey key, TValue value); void Add(TKey key, TValue value);
void Insert(int index, TKey key, TValue value);
} }
} }

View File

@@ -6,6 +6,10 @@ namespace Cryville.Common.Collections.Generic {
public void Add(object key, object value) { public void Add(object key, object value) {
Add(new KeyValuePair<object, object>(key, value)); Add(new KeyValuePair<object, object>(key, value));
} }
public void Insert(int index, object key, object value) {
Insert(index, new KeyValuePair<object, object>(key, value));
}
} }
public class PairList<TKey, TValue> : List<KeyValuePair<TKey, TValue>>, IPairList<TKey, TValue> { public class PairList<TKey, TValue> : List<KeyValuePair<TKey, TValue>>, IPairList<TKey, TValue> {
public void Add(TKey key, TValue value) { public void Add(TKey key, TValue value) {
@@ -20,5 +24,18 @@ namespace Cryville.Common.Collections.Generic {
throw new ArgumentException("Wrong key type or value type"); throw new ArgumentException("Wrong key type or value type");
} }
} }
public void Insert(int index, TKey key, TValue value) {
Insert(index, new KeyValuePair<TKey, TValue>(key, value));
}
public void Insert(int index, object key, object value) {
try {
Insert(index, (TKey)key, (TValue)value);
}
catch (InvalidCastException) {
throw new ArgumentException("Wrong key type or value type");
}
}
} }
} }