首页 新闻 赞助 找找看

像这样只是类型不同的方法可以优化吗?

0
悬赏园豆:5 [已解决问题] 解决于 2017-03-20 15:17
 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;
        }

 

可以看到代码中代码基本全部都重复,可是像这样只是类型不同的方法,又该怎么去优化呢?

跪求大神指教

临冰听雪丶的主页 临冰听雪丶 | 初学一级 | 园豆:191
提问于:2017-03-20 15:01
< >
分享
最佳答案
0

泛型啊``

收获园豆:5
吴瑞祥 | 高人七级 |园豆:29449 | 2017-03-20 15:08

噢。。泛型还可以这样用啊。。。谢谢啦

临冰听雪丶 | 园豆:191 (初学一级) | 2017-03-20 15:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册