fa

通过允许指定泛型类或方法操作的特定类型,泛型功能将类型安全的任务从您转移给了编译器。不需要编写代码来检测数据类型是否正确,因为会在编译时强制使用正确的数据类型。减少了类型强制转换的需要和运行时错误的可能性。
 private void StatusList(string id)
{
        List
<string> slist = cj.StatusList(id);
}

  public List<string> StatusList(string id)
{
            List
<string> list = new List<string>();

  list.Add(
"1");
//.
 return list;
}
**************************************************************************************

泛型提供了类型安全但没有增加多个实现的开销。例如,可以使用下面的变量声明创建字符串的链接列表:

LinkedList<string> llist = new LinkedList<string>();
不需要从基类型继承,也不需要重写成员。链接列表可以立即使用。有关 .NET Framework 提供的泛型集合类型,请参见 System.Collections.GenericSystem.Collections.ObjectModel

除类型安全外,由于不需要对值类型进行装箱,泛型集合类型对于存储和操作值类型执行的效果更好
泛型委托启用类型安全回调而无需创建多个委托类。例如,Predicate 泛型委托允许创建对特定类型实现您自己的搜索条件的方法,并允许将您的方法与 Array 类型的方法(如 FindFindLastFindAll)一起使用。
Array.Find方法:
private void StatusList(string id)
{


        System.Drawing.Point[] points 
=new System.Drawing.Point(100200), 
            
new System.Drawing.Point(150250), new System.Drawing.Point(250375), 
            
new System.Drawing.Point(275395), new System.Drawing.Point(295450) }
;
        System.Drawing.Point first 
= Array.Find(points, ProductGT10);
        Response.Write(first.X 
+ "<br>" + first.Y);
}
 private static bool ProductGT10(System.Drawing.Point p)
{
        
if (p.X * p.Y > 100000)
        
{
            
return true;
        }

        
else
        
{
            
return false;
        }

}

posted on 2007-11-12 13:53  simhare  阅读(203)  评论(0)    收藏  举报

导航