标题:C#两个泛型数据能实现相加吗?
取消只看楼主
hou0876
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-10-27
 问题点数:0 回复次数:1 
C#两个泛型数据能实现相加吗?
public  class Generic<T>
    {

        public  T MySum( List<T> listInt)
        {
            Func<List<T>, int,T> S = null;

            S = (list, x) =>
            {
                if (x == 1) { return list[x-1]; }
                else
                {
                  return Add(S(list, x - 1),list[x - 1]);
                  
                }
            };
            return S(listInt, listInt.Count);
        }

        public T Add(T num1, T num2)
        {
            var para1 = Expression.Parameter(typeof(T), "p1");

            var para2 = Expression.Parameter(typeof(T), "p2");

            var expsAdd = Expression<Func<T, T, T>>.Lambda<Func<T, T, T>>
                        (Expression.Add(para1, para2));

            Func<T, T, T> fun = ();

            return fun(num1, num2);
        }
    }

 class Program
 {
       static void Main(string[] args)
        {
           var R = new Generic<int>();
           var listInt=new List<int>{3,4};
           //R.MySum(listInt);
           //R.Add(2, 6);
          Console.WriteLine(R.Add(listInt));
          Console.WriteLine(R.Add(2, 6));
         }
 }
小弟是学生正在学习C#..
我没能实现两个泛型相加!!求高手来解决!!
搜索更多相关主题的帖子: public return null 
2011-11-04 20:12
hou0876
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-10-27
得分:0 
问题解决啦~~
2011-11-07 18:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-354316-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.052665 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved