hercules135 2015-01-07 03:24 采纳率: 0%
浏览 2154
已采纳

泛型T 如何在扩展方法中使用?

本来想了解一下扩展方法,但是看到文库中的实例明显有问题,特来提问
泛型对象T 如何使用Length 属性?
可以像类中一样加继承于某某类的约束吗?

http://wenku.baidu.com/link?url=Vz-Cjrq24tvblbk0l1I4IPYYfZ82jkGpzGBoNBDhi6qSCJko9hSTcrKGWrunO91TqTgyqB0AMvueHUP1or-mi8ktWMejceDW6Y-OphMnBh7

 public static class testEx
    {
        public static int ToInt32haha(this string str)
        {
            return Int32.Parse(str);
        }
        public static T[] Slice<T>(this T source, int index, int count)
        {
            if (index < 0 || count < 0 || index + count > source.Length)
            {
                throw new ArgumentException();
            }
            T[] result = new T[count];
            Array.Copy(source, index, result, 0, count);
            return result; 
        }
    }
  • 写回答

6条回答 默认 最新

  • threenewbee 2015-01-07 03:46
    关注
             public static T[] Slice<T>(this IEnumerable<T> source, int index, int count)
            {
                if (index < 0 || count < 0 || index + count > source.Count())
                {
                    throw new ArgumentException();
                }
                T[] result = new T[count];
                Array.Copy(source.ToArray(), index, result, 0, count);
                return result;
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样