ReversibleSortedList 0.2版本:添加了實現IComparer<T>接口的內部類
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
public class ReversibleSortedList<TKey, TValue>
{
#region 成員變量
private TKey[] keys=new TKey[0]; //鍵數組
private TValue[] values; //值數組
private static TKey[] emptyKeys;
private static TValue[] emptyValues;
#endregion
#region 構造方法
//類型構造器
static ReversibleSortedList()
{
ReversibleSortedList<TKey, TValue>.emptyKeys = new TKey[0];
ReversibleSortedList<TKey, TValue>.emptyValues = new TValue[0];
}
public ReversibleSortedList()
{
this.keys = ReversibleSortedList<TKey, TValue>.emptyKeys;
this.values = ReversibleSortedList<TKey, TValue>.emptyValues;
}
#endregion
#region 公有屬性
public int Capacity //容量屬性
{
get
{
return this.keys.Length;
}
}
#endregion
#region SortDirectionComparer類定義
public class SortDirectionComparer<T> : IComparer<T>
{ //ListSortDirection 枚舉,有兩個值:
//Ascending按升序排列,Descending按降序排列
private System.ComponentModel.ListSortDirection _sortDir;
//構造方法
public SortDirectionComparer()
{ //默認為升序
_sortDir = ListSortDirection.Ascending;
}
//可指定排序方向的構造方法
public SortDirectionComparer(ListSortDirection sortDir)
{
_sortDir = sortDir;
}
//排序方向屬性
public System.ComponentModel.ListSortDirection SortDirection
{
get { return _sortDir; }
set { _sortDir = value; }
}
//實現IComparer<T>接口的方法
public int Compare(T lhs, T rhs)
{
int compareResult =
lhs.ToString().CompareTo(rhs.ToString());
// 如果是降序,則反轉.
if (SortDirection == ListSortDirection.Descending)
compareResult *= -1;
return compareResult;
}
}
#endregion
}
public class Test
{
static void Main()
{
ReversibleSortedList<int, string> rs=new ReversibleSortedList<int, string>();
Console.WriteLine(rs.Capacity);
}
}