zoukankan      html  css  js  c++  java
  • asp.net数据类型--泛型

     asp.net有很多的数据类型,同时c#等均是强数据类型,在使用的过程,存在因数据类型不一致,在编译时通过,在使用过程中出错的情况,因此从2.0起,增加泛型这种类型。这种类型,在定义时不指定类型,而在使用过程指定类型。

    using命名空间

    using system.collections.generic /*Generic:泛型*/

    常用的系统已定义的类型

    1、dictionary(字典)类

    在dictionary泛型中,后面有两个string参数,在系统定义为TKey、KValue(即键值对),其使用方法为:

    dictinary<string,string> dic=new dictionary();

    说明:在泛型使用中,具体的数据类型用<>表示。

    在dictionary中有很多的方法来进行赋值及取值操作。

    (1)add:增加键值对

    dic.add("13","aaa");

    dic.add("14","bbb");

    (2)keyvaluepair/*键值对*/取值

    foreach(keyvaluepqir kvp in div)

    {Response.Write(kvp.Key);

    Response.Write(kvp.Value);}

    (3)ContainKey/ContainValue:判断是否有某个键值或值

    if(!dic.Contain("13"))

    {dic.add("13","aaa");} 

    (4)TryGetValue:根据键值取值

    string sValue=string.empty();

    dic.TryGetValue("13",out sValue);/*注意在此处有一输入参数out*/

    Response.Write(sValue);

    2、list<>类

     如list<int> iList=new list<int>();与list<double> dList=new list<double>();是不一样的两个数据,通过这样的定义,就能够保证类型安全。

    list<int> iList=new list<int>();

    iList.add(10);

    iList.add(20);

    iList.add(40.0);/*在进行编译时,此行出错,同时计算的结果不包含此数据*/

    int sum=0;

    foreach( int i in iList)

    {sum=sum+i;}

    Response.Write(sum);

    3、泛型IList<>类

    与List集合对象相同。

  • 相关阅读:
    Qt进程间通信
    reinterpret
    vs调试技巧
    利用QSystemSemaphore和QSharedMemory实现进程间通讯
    QLocalSocket
    QShareMemory
    qt动态库实现无边框窗体的消息处理 nativeEvent的使用
    BCB6常用快捷键
    1219个人总结
    冲刺二 12.6
  • 原文地址:https://www.cnblogs.com/wdcwy/p/4855017.html
Copyright © 2011-2022 走看看