第一次泛型编程(以前从来也只有用过List<T>之类的)
不知道会有什么问题呢?继承IList的部分没有注释了,MSDN可以查的。欢迎提意见噢~
你可能会看到下面的类用多种方式实现了同样的效果,有多态,有模版,还有包装wrapper(不知道是不是恰当)……还用了泛型……
实际项目中,如果有可取之处,请只取其中一种就可以了……(计划中……)
补充一张VS自动生成的类图
using System;
using System.Collections.Generic;
using System.Collections;

namespace CA_PairCollection


{

/**//// <summary>
/// 基础格式化机制
/// </summary>
/// <typeparam name="T"></typeparam>
interface IFormat<T>

{
T GetFormat();
}


/**//// <summary>
/// 格式化模板接口
/// </summary>
interface IFormatTemplate

{

/**//// <summary>
/// Text模版,形如:"Text = "
/// </summary>

string TextTemplate
{ get;set;}

/**//// <summary>
/// Value模版,形如:"Value = "
/// </summary>

string ValueTemplate
{ get;set;}

/**//// <summary>
/// 分隔符模版,形如:","
/// </summary>

string Separator
{ get;set;}
}


/**//// <summary>
/// 提供用于格式化对象的模板的格式化机制
/// </summary>
interface IFormatProvider

{
string GetFormat(IFormatTemplate formatTemplate);
}


/**//// <summary>
/// 格式化模板类
/// </summary>
class FormatTemplate : IFormatTemplate

{
public FormatTemplate(string textTemplate,string separator,string valueTemplate)

{
this.textTemplate = textTemplate;
this.separator = separator;
this.valueTemplate = valueTemplate;
}

private string textTemplate;
private string separator;
private string valueTemplate;

IFormatTemplate 成员#region IFormatTemplate 成员

public string TextTemplate

{
get

{
return this.textTemplate;
}
set

{
this.textTemplate = value;
}
}

public string ValueTemplate

{
get

{
return this.valueTemplate;
}
set

{
this.valueTemplate = value;
}
}

public string Separator

{
get

{
return this.separator;
}
set

{
this.separator = value;
}
}

#endregion
}


/**//// <summary>
/// 成对参数类
/// </summary>
/// <typeparam name="U"></typeparam>
/// <typeparam name="V"></typeparam>
class Pair<U, V> : IFormat<string>, IFormatProvider

{

public Pair()
{ }
public Pair(U text, V value)

{
this.text = text;
this.value = value;
}

private U text;
public U Text

{
get

{
return this.text;
}
set

{
this.text = value;
}
}

private V value;
public V Value

{
get

{
return this.value;
}
set

{
this.value = value;
}
}


IFormat 成员#region IFormat<string> 成员

public virtual string GetFormat()

{
return "Text = " + this.Text.ToString() + "; Value = " + this.Value.ToString();
}

#endregion


IFormatProvider 成员#region IFormatProvider 成员

public virtual string GetFormat(IFormatTemplate formatTemplate)

{
return formatTemplate.TextTemplate + this.Text.ToString()
+ formatTemplate.Separator
+ formatTemplate.ValueTemplate + this.Value.ToString();
}

#endregion

}


/**//// <summary>
/// 成对参数类集合
/// </summary>
/// <typeparam name="U"></typeparam>
/// <typeparam name="V"></typeparam>
class PairCollection<U, V> : IList<Pair<U, V>>, IFormat<string>, IFormatProvider

{
private IList<Pair<U, V>> pairs;

public PairCollection()

{
pairs = new List<Pair<U, V>>();
}

IListPairU,V 成员#region IList<Pair<U,V>> 成员

public int IndexOf(Pair<U, V> item)

{
return this.pairs.IndexOf(item);
}

public void Insert(int index, Pair<U, V> item)

{
this.pairs.Insert(index,item);
}

public void RemoveAt(int index)

{
this.pairs.RemoveAt(index);
}

public Pair<U, V> this[int index]

{
get

{
return this.pairs[index];
}
set

{
this.pairs[index]=value;
}
}

#endregion


ICollectionPairU,V 成员#region ICollection<Pair<U,V>> 成员

public void Add(Pair<U, V> item)

{
this.pairs.Add(item);
}

public void Clear()

{
this.pairs.Clear();
}

public bool Contains(Pair<U, V> item)

{
return this.pairs.Contains(item);
}

public void CopyTo(Pair<U, V>[] array, int arrayIndex)

{
this.pairs.CopyTo(array,arrayIndex);
}

public int Count

{
get

{
return this.pairs.Count;
}
}

public bool IsReadOnly

{
get

{
return this.IsReadOnly;
}
}

public bool Remove(Pair<U, V> item)

{
return this.Remove(item);
}

#endregion


IEnumerablePairU,V 成员#region IEnumerable<Pair<U,V>> 成员

public IEnumerator<Pair<U, V>> GetEnumerator()

{
return this.pairs.GetEnumerator();
}

#endregion


IEnumerable 成员#region IEnumerable 成员

IEnumerator IEnumerable.GetEnumerator()

{
return (IEnumerator)GetEnumerator();
}

#endregion


IFormat 成员#region IFormat 成员

public virtual string GetFormat()

{
string result = string.Empty;
foreach (Pair<U, V> p in this.pairs)

{
result = result + p.GetFormat() + "\r\n";
}
return result;
}

#endregion


IFormatProvider 成员#region IFormatProvider 成员

public string GetFormat(IFormatTemplate formatTemplate)

{
string result = string.Empty;
foreach (Pair<U, V> p in this.pairs)

{
result = result + p.GetFormat(formatTemplate) + "\r\n";
}
return result;
}

#endregion
}


/**//// <summary>
/// 实现了中文式成对参数的类
/// </summary>
/// <typeparam name="U"></typeparam>
/// <typeparam name="V"></typeparam>
sealed class ChinesePair<U, V> : Pair<U, V>

{
public ChinesePair()
: base()

{ }
public ChinesePair(U text,V value)
: base(text, value)

{ }
public override string GetFormat()

{
return "文本 = " + this.Text.ToString() + "; 值 = " + this.Value.ToString();
}
}


/**//// <summary>
/// 实现了英文式成对参数的类
/// </summary>
/// <typeparam name="U"></typeparam>
/// <typeparam name="V"></typeparam>
sealed class EnglishPair<U, V> : Pair<U, V>

{
public EnglishPair()
: base()

{ }
public EnglishPair(U text, V value)
: base(text, value)

{ }
public override string GetFormat()

{
return "Text = " + this.Text.ToString() + "; Value = " + this.Value.ToString();
}
}


/**//// <summary>
/// 键值均为string类型的一个类型包装
/// </summary>
class KVPairCollection : PairCollection<string, string>,IFormat<string>

{
private PairCollection<string, string> p;
public KVPairCollection()

{
p = new PairCollection<string, string>();
}


IFormat 成员#region IFormat<string> 成员

string IFormat<string>.GetFormat()

{
return p.GetFormat();
}

#endregion
}

class Program

{
static void Main(string[] args)

{
//
//以下示例用多种方式实现了成对类的格式化输出
//

PairCollection<string, string> psBases = new PairCollection<string, string>();
psBases.Add(new Pair<string, string>("baseText1", "baseValue1"));
psBases.Add(new Pair<string, string>("baseText2", "baseValue2"));
foreach (Pair<string, string> p in psBases)

{
Console.WriteLine(p.GetFormat());
}

Console.WriteLine("----------------------");

PairCollection<string, string> psPloyStrings = new PairCollection<string, string>();
psPloyStrings.Add(new ChinesePair<string, string>("textChinese1", "valueChinese1"));
psPloyStrings.Add(new EnglishPair<string, string>("textEnglish1", "valueEnglish1"));
foreach (Pair<string, string> p in psPloyStrings)

{
Console.WriteLine(p.GetFormat());
}

Console.WriteLine("----------------------");

PairCollection<string, int> psPloyInts = new PairCollection<string, int>();
psPloyInts.Add(new ChinesePair<string, int>("textChinese1", 1));
psPloyInts.Add(new EnglishPair<string, int>("textEnglish1", 2));
foreach (Pair<string, int> p in psPloyInts)

{
Console.WriteLine(p.GetFormat());
}

Console.WriteLine("----------------------");

Console.WriteLine(psPloyStrings.GetFormat());
Console.WriteLine(psPloyInts.GetFormat());

Console.WriteLine("----------------------");

KVPairCollection kvs = new KVPairCollection();
kvs.Add(new ChinesePair<string, string>("textChinese1", "valueChinese1"));
kvs.Add(new EnglishPair<string, string>("textEnglish1", "valueEnglish1"));
kvs.Add(new Pair<string, string>("textBase1", "valueBase"));
Console.WriteLine(kvs.GetFormat());

Console.WriteLine("----------------------");

PairCollection<string, string> pFormatProvider = new PairCollection<string, string>();
pFormatProvider.Add(new ChinesePair<string, string>("textChinese1", "valueChinese1"));
pFormatProvider.Add(new EnglishPair<string, string>("textEnglish1", "valueEnglish1"));
Console.WriteLine(pFormatProvider.GetFormat((IFormatTemplate)(new FormatTemplate("模版Text = "," ; ", "模版Value = "))));
}
}
}
