public static int[] DataDifference(int[] more, int[] few) { //差异Id var sbuNoItapSessionId = new int[more.Length - few.Length]; var indexi = 0; for (int i = 0; i < more.Length; i++) { var isHave = false; for (int j = 0; j < few.Length; j++) { if (more[i].Equals(few[j])) { isHave = true; break; } } if (!isHave) { sbuNoItapSessionId[indexi] = more[i]; indexi++; } } return sbuNoItapSessionId; } public static string[] DataDifference(string[] more, string[] few) { //差异Id var sbuNoItapSessionId = new string[more.Length - few.Length]; var indexi = 0; for (int i = 0; i < more.Length; i++) { var isHave = false; for (int j = 0; j < few.Length; j++) { if (more[i].Equals(few[j])) { isHave = true; break; } } if (!isHave) { sbuNoItapSessionId[indexi] = more[i]; indexi++; } } return sbuNoItapSessionId; } public static ResultMessage StitchingAndReturnsAnArray(int[] datas) { var noIds = new StringBuilder(); var result = new ResultMessage(); if (datas != null && datas.Count() != 0) { for (int i = 0; i < datas.Length; i++) { if (i > 0) { noIds.Append(","); } noIds.Append(datas[i]); } result.Success = "false"; result.Message = noIds.ToString(); } else { result.Success = "true"; } return result; } public static ResultMessage StitchingAndReturnsAnArray2(string[] datas) { var noIds = new StringBuilder(); var result = new ResultMessage(); if (datas != null && datas.Count() != 0) { for (int i = 0; i < datas.Length; i++) { if (i > 0) { noIds.Append(","); } noIds.Append(datas[i]); } result.Success = "false"; result.Message = noIds.ToString(); } else { result.Success = "true"; } return result; }
可以看到代码中代码基本全部都重复,可是像这样只是类型不同的方法,又该怎么去优化呢?
跪求大神指教
泛型啊``
噢。。泛型还可以这样用啊。。。谢谢啦