各位前辈,帮忙给看一下,泛型方法与不限数量的参数与数组怎样结合使用? 本帖最后由 ScholarLin 于 2015-02-07 15:56:12 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 where T : class 去掉all = StaticClass.MergeArray<int>(a, b, c); 看看人家 .net framework 源代码是怎样合并的: public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second) { if (first == null) { throw Error.ArgumentNull("first"); } if (second == null) { throw Error.ArgumentNull("second"); } return Enumerable.ConcatIterator<TSource>(first, second); } private static IEnumerable<TSource> ConcatIterator<TSource>(IEnumerable<TSource> first, IEnumerable<TSource> second) { foreach (TSource tSource in first) { yield return tSource; } foreach (TSource tSource1 in second) { yield return tSource1; } } 调用 Linq 时,就是这样的:int[] a = new int[] { 1, 2, 3 } , b = new int[] { 4, 5, 6 } , c = new int[] { 7, 8, 9 };var all = a.Concat(b).Concat(c).ToArray(); public static class ArrayTest<T> { /// <summary> /// 合并数组 /// </summary> /// <param name="args"></param> /// <returns></returns> public static T[] MerageArr(params T[][] args) { int arraryLength = 0, len = 0; foreach (var item in args) { arraryLength += item.Length; } T[] result = new T[arraryLength]; foreach (var item in args) { item.CopyTo(result, len); len += item.Length; } return result; } }调用 int[] a = new int[3] { 1, 2, 3 }; int[] b = new int[3] { 4, 5, 6 }; int[] c = ArrayTest<int>.MerageArr(a, b); StringBuilder sb = new StringBuilder("合并后的数组:"); foreach (int i in c) { sb.AppendFormat(",{0}", i); } MessageBox.Show(sb.ToString()); 关于excel倒数数据库的问题 c#的WinForm程序怎样镶嵌在桌面上,具体应该怎么写? 如何快速校验大型文件? 一个FileStream的文件 请教 怎么把datagridview中的数据做成一定格式的报表? 大家周六在上班吗? vb实现远程开机 如何让listview控件的每个item都在独立的一行上显示,而不是根据宽度自适应! 重复绑定问题 C#项目发布问题 winfrom textbox 中输入如1/3 这样的带有除号的数,如何将它转化成double类型? 关于WPF 访问INI的问题
all = StaticClass.MergeArray<int>(a, b, c);
{
if (first == null)
{
throw Error.ArgumentNull("first");
}
if (second == null)
{
throw Error.ArgumentNull("second");
}
return Enumerable.ConcatIterator<TSource>(first, second);
}
private static IEnumerable<TSource> ConcatIterator<TSource>(IEnumerable<TSource> first, IEnumerable<TSource> second)
{
foreach (TSource tSource in first)
{
yield return tSource;
}
foreach (TSource tSource1 in second)
{
yield return tSource1;
}
}
, b = new int[] { 4, 5, 6 }
, c = new int[] { 7, 8, 9 };
var all = a.Concat(b).Concat(c).ToArray();
public static class ArrayTest<T>
{
/// <summary>
/// 合并数组
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
public static T[] MerageArr(params T[][] args)
{
int arraryLength = 0, len = 0;
foreach (var item in args)
{
arraryLength += item.Length;
} T[] result = new T[arraryLength];
foreach (var item in args)
{
item.CopyTo(result, len);
len += item.Length;
}
return result;
}
}
调用 int[] a = new int[3] { 1, 2, 3 };
int[] b = new int[3] { 4, 5, 6 };
int[] c = ArrayTest<int>.MerageArr(a, b);
StringBuilder sb = new StringBuilder("合并后的数组:");
foreach (int i in c)
{
sb.AppendFormat(",{0}", i);
}
MessageBox.Show(sb.ToString());