【发布时间】:2011-10-20 04:11:58
【问题描述】:
我有一些方法实际上是相同的,只是它们具有不同数量的泛型类型参数。内部代码非常非常相似:
public Set[] CreateSet<TFirst, TSecond>(List<TFirst> first, List<TSecond> second)
{
Set[] result = new Set[2];
result[0] = CreateSet(first);
result[1] = CreateSet(second);
return result;
}
public Set[] CreateSet<TFirst, TSecond, TThird>(List<TFirst> first, List<TSecond> second, List<TThird> third)
{
Set[] result = new Set[3];
result[0] = CreateSet(first);
result[1] = CreateSet(second);
result[2] = CreateSet(third);
return result;
}
...
等等。我有这些方法最多 7 个泛型类型参数。正如您所看到的,它们实际上都相同,只是它们创建了不同大小的集合数组。
我不喜欢这种代码重复,所以是否可以将此代码重构为一个私有方法,这些方法将在内部调用。或者我应该考虑其他方式吗?
【问题讨论】:
标签: generics .net-3.5 c#-3.0 refactoring