1. array是一个固定长度的,如果要动态的存储的话就不行了,虽然 System.Collections.ArrayList(),是一个动态的存储的容器,但是没有对存储中的数据进行一个约束,所以非泛型的容器和泛型 的容器相比存在两个问题:
                          1.性能问题;
                          2.安全问题;
=========================================================================================================
2.1.非泛型的命名空间:System.Collections    与   System.Collections.Specialized;

2.2.泛型命名空间:System.Collections.Generic;

=========================================================================================================
3.1.System.Collections.Generic.List<T>:表示可通过索引访问的对象的强类型列表。 提供用于对列表进行搜索、排序和操作的方法。
建立一个CZuigao类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    class CZuigao
    {
        //2个私有字段用来存储最高分学生的姓名和分数:
        private string name;
        private float score;
        //姓名和分数的属性:
        public string Name
        {
            get { return name; }
            set { if( value.Length > 1 ) { name = value; } }
        }
        public float Score
        {
            get { return score; }
            set { if( float.Parse(value.ToString()) > 0.1f ) { score = (float)value; } }
        }
        //显示存储在私有字段中的信息(姓名 分数);
        public void printf()
        {
            Console.WriteLine("最高分为:{1:f},最高分学生姓名是:{0}",name ,score );
        }
        //构造函数:
        public CZuigao() { }
        public CZuigao(string n, float f)
        {
            Name = n;
            Score = f;
        }
        public CZuigao(string nn)
            : this(nn, 0.11f) { }
        public CZuigao(float ff)
            : this("没有啊!", ff) { }
    }
}

main():

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //调用3个内部构造函数:
            CZuigao cz = new CZuigao("李异峰");
            cz.printf();
            CZuigao cz1 = new CZuigao(2.3f);
            cz1.printf();
            CZuigao cz2 = new CZuigao("李晓峰", 3.6f);
            cz2.printf();
            //直接初始化赋值属性:
            CZuigao cz3 = new CZuigao() { Name = "我是属性!",Score=8.8f };
            cz3.printf();
            Console.WriteLine("下面是list<t>:");
            //list<T>:
            Program pro = new Program();
            List<CZuigao> lzg = new List<CZuigao>() {new CZuigao("list1",1.1f),new CZuigao("list2",2.1f),new CZuigao("list3",3.3f) };
            foreach( CZuigao scz in lzg ) {
                scz.printf();
            }
            Console.WriteLine("\n添加CZuigao对象后:");
            lzg.Add(cz );
            foreach( CZuigao scz in lzg ) {
                scz.printf();
            };

            Console.ReadLine();

        }
    }

}

F5:

3.2.System.Collections.Generic.Stack<T>类是:表示相同任意类型的实例的可变大小的后进先出 (LIFO) 集合。
依然利用CZuigao类进行输入输出:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //调用3个内部构造函数:
            CZuigao cz = new CZuigao("李异峰");
            cz.printf();
            CZuigao cz1 = new CZuigao(2.3f);
            cz1.printf();
            CZuigao cz2 = new CZuigao("李晓峰", 3.6f);
            cz2.printf();
            //直接初始化赋值属性:
            CZuigao cz3 = new CZuigao() { Name = "我是属性!",Score=8.8f };
            cz3.printf();
            Console.WriteLine("\n下面是stack<t>:");
            //stack<T>:
            Stack<CZuigao> szg = new Stack<CZuigao>();
            szg.Push(new CZuigao("stack1", 9.8f));
            szg.Push(new CZuigao("stack2",9.2f));
            Console.WriteLine("最后输入的姓名:{0},分数是:{1}",szg.Peek().Name,szg.Peek().Score.ToString() );
            //添加CZuigao类的对象后:
            Console.WriteLine("\n下面是stack<t>添加CZuigao对象后的顶点:");
            szg.Push(cz );
            Console.WriteLine("顶点:姓名:{0},分数是:{1}", szg.Peek().Name, szg.Peek().Score.ToString());
            //移除2条后:
            szg.Pop();
            szg.Pop();
            Console.WriteLine("\n下面是stack<t>移除CZuigao的2条后的顶点:");
            Console.WriteLine("顶点:姓名:{0},分数是:{1}", szg.Peek().Name, szg.Peek().Score.ToString());
            //在移除后:
            try {
                szg.Pop();
                Console.WriteLine("顶点:姓名:{0},分数是:{1}", szg.Peek().Name, szg.Peek().Score.ToString());
            }
            catch(Exception e)
            {
                Console.WriteLine("\n\nszg里面为null,详情如下:{0}",e.ToString());
            }
            Console.ReadLine();

        }
    }

}

F5:

=========================================================================================================
3.3.System.Collections.Generic.Queue<T>类与上面的stack类方法相反为:表示对象的先进先出集合。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //调用3个内部构造函数:
            CZuigao cz = new CZuigao("李异峰");
            cz.printf();
            CZuigao cz1 = new CZuigao(2.3f);
            cz1.printf();
            CZuigao cz2 = new CZuigao("李晓峰", 3.6f);
            cz2.printf();
            //直接初始化赋值属性:
            CZuigao cz3 = new CZuigao() { Name = "我是属性!",Score=8.8f };
            cz3.printf();
            Console.WriteLine("\n下面是Queue<T>:");
            //Queue<T>:
            //初始化:
            Queue<CZuigao> qzg = new Queue<CZuigao>();
            qzg.Enqueue(new CZuigao("queue1", 5.1f));
            qzg.Enqueue(new CZuigao("queue2",5.2f));
            //返回顶点:
            Console.WriteLine("顶点上的姓名是:{0},分数是:{1:f}",qzg.Peek().Name ,qzg.Peek().Score);
            //添加上面CZuigao类的对象:
            qzg.Enqueue(cz );
            //返回顶点:
            Console.WriteLine("\n添加CZuIgao对象后:\n顶点上的姓名是:{0},分数是:{1:f}", qzg.Peek().Name, qzg.Peek().Score);
            //移除顶点:
            qzg.Dequeue();
            //返回顶点:
            Console.WriteLine("\n移除最高点后:\n顶点上的姓名是:{0},分数是:{1:f}", qzg.Peek().Name, qzg.Peek().Score);
            Console.ReadLine();

        }
    }

}

F5:

=========================================================================================================
3.4.System.Collections.Generic.SortedSet<T>,表示按排序顺序保持的对象的集合。
首先要实现IComparer接口的Compare()方法:

public class IComparse:IComparer <CZuigao >
    {
        #region IComparer<CZuigao> 成员
        int IComparer<CZuigao>.Compare(CZuigao x, CZuigao y)
        {
            if( x.Score > y.Score ) { return 1; }
            if( y.Score > x.Score ) { return -1; }
            else { return 0; }
        }
        #endregion
    }

再main():

  static void Main(string[] args)
        {
            //初始化sortedset<T>:
            //***************注意啊 :在Generic.SortedSet类中要初始化构造函数****************************
            System.Collections.Generic.SortedSet<CZuigao> sorzg = new SortedSet<CZuigao>(new IComparse());
            //初始化5个CZuigao的对象:
            Queue<CZuigao> qzg = new Queue<CZuigao>();
            for( int i = 0; i < 5; i++ ) {
                qzg.Enqueue(new CZuigao(string.Format("sortedset{0:d}", i), float.Parse(i.ToString())));
            }
            //将Queue<CZuigao> qzg中的数据复制到sorzg里面保存:
            foreach( CZuigao qq in qzg ) {
                sorzg.Add(qq);
            }
            //遍历sortedset对象中的数据:
            Console.WriteLine("初始化后的遍历:");
            foreach( CZuigao sor in sorzg ) { Console.WriteLine("name={0},\tscore={1:f}", sor.Name, sor.Score); }
            //添加一条数据对象:
            sorzg.Add(new CZuigao("sortedset5",3.1f));
            //遍历sortedset对象中的数据:
            //下面是添加后的遍历:
            Console.WriteLine("\n下面是添加后的遍历:");
            foreach( CZuigao sor in sorzg ) { Console.WriteLine("name={0},\tscore={1:f}", sor.Name, sor.Score); }
            Console.ReadLine();
        }

F5后的数据比较:

这样排序保存主要的是IComparer接口的Compare()方法实现的;
现在还可以对其进行修改后看顶点:

            //移除最高点操作:
            sorzg.Remove((CZuigao)sorzg.Max );
            Console.WriteLine("\n当前sortedset内共有:{0:d}个对象数据;\n下面是移除最高点后的遍历:",sorzg.Count);
            foreach( CZuigao sor in sorzg ) { Console.WriteLine("name={0},\tscore={1:f}", sor.Name, sor.Score); }

F5:

========================================================================================================
3.5.
在System.Collections.ObjectModel空间下有个
System.Collections.ObjectModel.ObservableCollection<T>类,表示一个动态数据集
合,在添加项、移除项或刷新整个列表时,此集合将提供通知。

其用法和list<t>类似:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 //using System.Collections;
 //using System.Collections.Specialized;
 using System.Collections.ObjectModel;
 namespace ConsoleApplication1
 {
     class Program
     {
         static void Main(string[] args)
         {
            ObservableCollection<CZuigao> obzg = new ObservableCollection<CZuigao>() {new CZuigao ("observablecollection1",3.1f) };

             obzg.CollectionChanged+=obzg_CollectionChanged;
             //添加对象:
             obzg.Add(new CZuigao("observablecollection2", 8.1f));
             //遍历当前所有的对象数据:
             Console.WriteLine("\n当前所有的对象数据:");
             foreach( CZuigao ss in obzg )
             {
                 Console.WriteLine("name={0},\tscore={1:f}",ss.Name ,ss.Score );
             }
             Console.WriteLine("\n");
             //移除:
             obzg.RemoveAt();
             Console.ReadLine();
         }
         //public enum NotifyCollectionChangedAction
         //{
         //  Add = 0,
         //  Remove = 2,
         //}
 private static void obzg_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
 {
                 if( e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add  )
             {
                 Console.WriteLine("用户刚刚操作是添加操作,共添加了{0:d}个对象数据,详情如下:",e.NewItems.Count );
                 foreach( CZuigao ss in e.NewItems )
                 {
                     Console.WriteLine("name={0},\tscore={1:f}",ss.Name,ss.Score  );
                 }
             }
             if( e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove )
             {
                 Console.WriteLine("是移除操作,共移除{0:d}个对象数据,详情如下:",e.OldItems .Count );
                 foreach( CZuigao ss in e.OldItems  )
                 {
                     Console.WriteLine("name={0},\tscore={1:f}", ss.Name, ss.Score);
                 }
             }
 }

     }

 }

F5:

=========================================================================================================
3.6.实际中还可以自定义generic方法:
写一个调换方法:
                            对值类型进行调换:

         public static void Cdiaohuan<T>(ref T zg1,ref T zg2)
         {
             //下面这几行代码就是用来实现对象之间调换问题:
             //首先就需要定义一个临时的对象存储变量:
             T Temp;
             Temp = zg1;
             zg1 = zg2;
             zg2 = Temp;
         }

main():

         static void Main(string[] args)
         {
         //用值类型来进行调换:
             , b = ;
             Cdiaohuan<Int32>(ref a ,ref b );
             //输出a,b:
             Console.WriteLine("a is {0:d}\tb is {1:d}",a ,b );
             Console.ReadLine();
         }

F5就不截屏了;
                         对引用类型:CZuigao的对象进行调换:

         static void Main(string[] args)
         {
             // 对引用类型:CZuigao的对象进行调换:
             CZuigao zg1 = new CZuigao() {Name="object1",Score=1.1f };
             CZuigao zg2 = new CZuigao() {Name="object2",Score=2.2f };
             //输出对象中的数据:
             Console.WriteLine("zg1对象中的:name={0}\tscore={1:f}",zg1.Name ,zg1.Score );
             Console.WriteLine("zg2对象中的:name={0}\tscore={1:f}",zg2.Name ,zg2.Score );
             //调用方法:
             Cdiaohuan<CZuigao>(ref zg1, ref zg2);
             Console.WriteLine("\n下面就是执行方法后的对象中的数据值:");
             //输出对象中的数据:
             Console.WriteLine("zg1对象中的:name={0}\tscore={1:f}", zg1.Name, zg1.Score);
             Console.WriteLine("zg2对象中的:name={0}\tscore={1:f}", zg2.Name, zg2.Score);
             Console.ReadLine();
         }

F5:

当然了输出这两句话可以再写个方法进行输出,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

****还可以将generic应用到class和interface/struct/委托里,就是不能用在enum中:

写一个generic的class:

 namespace ConsoleApplication1
 {
     class CValue<T>
     {
         public T values1, values2;
         public void printf()
         {
             Console.WriteLine("values1 is {0}\nvalues2 is {1}",values1.ToString(),values2.ToString());
         }
     }
 }

在main()中实现:

         static void Main(string[] args)
         {
             CValue<Int32> cv = new CValue<int>();
             cv.values1 = ;
             cv.values2 = ;
             cv.printf();

             Console.ReadLine();
         }

=========================================================================================================
3.7.上面的这实例还是存在一定的漏洞,比如说上面3.6.中说的一个方法:public static void Cdiaohuan<T>(ref T zg1,ref T zg2),可以对值类型和引用类型就行调换,如果我们强制要求只能调换object类型呢?这样的话就要用到generic中的约束,where和sqlser中的where的意思有点类似筛选符合的要求:
看看3.6.中的   public static void Cdiaohuan<T>(ref T zg1,ref T zg2):

         public static void Cdiaohuan<T>(ref T zg1,ref T zg2)
         {
             //下面这几行代码就是用来实现对象之间调换问题:
             //首先就需要定义一个临时的对象存储变量:
             T Temp;
             Temp = zg1;
             zg1 = zg2;
             zg2 = Temp;
         }

添加个object约束:

         public static void Cdiaohuan<T>(ref T zg1,ref T zg2) where T :Class
         {
             //下面这几行代码就是用来实现对象之间调换问题:
             //首先就需要定义一个临时的对象存储变量:
             T Temp;
             Temp = zg1;
             zg1 = zg2;
             zg2 = Temp;
         }

如果还是让方法执行value的调换的话:

         static void Main(string[] args)
         {
             , b = ;
             try { Cdiaohuan<Int32>(ref a, ref b); }
             catch( Exception ex ) { Console.WriteLine(ex.ToString()); }

             Console.ReadLine();
         }

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
执行object类型的调换:
在此之前在 class CZuigao 完成接口的实现;     class CZuigao:IComparable <CZuigao > :

         #region IComparable<CZuigao> 成员
         int IComparable<CZuigao>.CompareTo(CZuigao other)
         {
             ; }
             ; }
             ; }
         }
         #endregion
         static void Main(string[] args)
         {
             //初始化List<T>,并添加2条对象数据:
             List<CZuigao> lzg = new List<CZuigao>() { new CZuigao("yueshu1", 1.1f), new CZuigao("yueshu2", 2.2f) };
             Console.WriteLine("List<T>中的对象数据条:");
             //遍历输出:
             foreach( CZuigao ss in lzg ) { Console.WriteLine("name={0}\tscore={1:f}", ss.Name, ss.Score); }
             CZuigao zg1 = (CZuigao)lzg.Min();
             CZuigao zg2 = (CZuigao)lzg.Max();
             //print当前CZuigao的zg1和zg2对象中的数据:
             Console.WriteLine("\nzg1:\nname={0}\tscore={1:f}",zg1.Name ,zg1.Score );
             Console.WriteLine("zg2:\nname={0}\tscore={1:f}\n", zg2.Name, zg2.Score);
             Cdiaohuan<CZuigao>(ref zg1 ,ref zg2 );
             //print执行Cdiaohuan()方法后的CZuigao的zg1和zg2对象中的数据:
             Console.WriteLine("\nzg1:\nname={0}\tscore={1:f}", zg1.Name, zg1.Score);
             Console.WriteLine("zg2:\nname={0}\tscore={1:f}\n", zg2.Name, zg2.Score);
                 Console.ReadLine();
         }

F5:

ok!
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
最后总结下where中有哪些约束类型:
1.where t:class       类型参数为object
2.where t:struct      类型参数为vale     type
3.where t:new()      必须包含一个构造函数
4.where t:baseClass      必须是它的派生类
5.where t:interface    必须实现该接口;

不能将类型参数运用在c#算术运算符(/*-+)上。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

完!

C#:system.collections.generic(泛型)的更多相关文章

  1. Web Service接口返回泛型的问题(System.InvalidCastException: 无法将类型为“System.Collections.Generic.List`1[System.String]”的对象强制转换为类型“System.String[]”)

    在使用C#写Web Service时遇到了个很奇怪的问题.返回值的类型是泛型(我用的是类似List<string>)的接口,测试时发现总是报什么无法转换为对象的错误,百思不得其解. 后来在 ...

  2. System.Collections.Generic的各容器类的用法

    演示System.Collections.Generic的各容器类的用法. 包括:Dictionary,KeyValuePair,SortedDic tionary,SortedList,HashSe ...

  3. NHibernate无法将类型“System.Collections.Generic.IList<T>”隐式转换为“System.Collections.Generic.IList<IT>

    API有一个需要实现的抽象方法: public IList<IPermission> GetPermissions(); 需要注意的是IList<IPermission>这个泛 ...

  4. using System.Collections.Generic;

    public class CommonClass { public static void ShowInt(int iValue) { //typeof(CommonClass) typeof关键字 ...

  5. C# System.Collections.Generic.Dictionary

    using System; using System.Collections.Generic; public class Example { public static void Main() { / ...

  6. System.Collections.Generic.List<T> 与 System.Collections.ArrayList

    [推荐] System.Collections.Generic.List<T> [原因] 泛型集合类List<T>在操作值类型的集合时可以不进行 装箱/拆箱 处理. 使得性能较 ...

  7. 无法将类型为“System.Windows.Controls.SelectedItemCollection”的对象强制转换为类型“System.Collections.Generic.IList`1

    在WPF中DataGrid 选择事件中获取SelectedItems 报错如下 无法将类型为“System.Windows.Controls.SelectedItemCollection”的对象强制转 ...

  8. Unity3d:Unknown type 'System.Collections.Generic.CollectionDebuggerView'1

    问题描述:如图,在调试状态下说:Unknown type 'System.Collections.Generic.CollectionDebuggerView'1<ignore_js_op> ...

  9. webservice asmx 无法序列化接口 System.Collections.Generic.IList

    转载自:http://www.cnblogs.com/chenhuzi/p/4178194.html 今天有位同事在方法里加了一个IList<entity> 的返回值,也没有测试,直接发布 ...

随机推荐

  1. Direct2D处理几何图形之间的碰撞检测(下)

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 上一篇文章中我们介绍了几何图形与点的碰撞检测.几何图形与点的位置关系比较简单:点在几何图形内.点在几何图形外.点 ...

  2. [Java] Design Pattern:Code Shape - manage your code shape

    [Java] Design Pattern:Code Shape - manage your code shape Code Shape Design Pattern Here I will intr ...

  3. CentOS安装log.io

    官网 http://logio.org/ log.io是运行在node.js之上的 可通过浏览器访问服务器日志 类似于tail -f命令 它只负责实时传输数据 而不会去存储历史数据 npm insta ...

  4. IOS git 删除仓库 新建仓库 提交 合并 操作 码云

    HDHaoShaoPengdeiMac:~ hdhaoshaopeng$ defaults write com.apple.finder AppleShowAllFiles TRUE HDHaoSha ...

  5. 关于linux下的命令

    1.文件和目录操作命令 pwd:显示当前的工作目录 cd:切换目录 tree:以树形结构图显示目录下的所有内容 mkdir:创建目录 touch:创建空文件或改变文件的时间戳属性 ls:显示目录下的内 ...

  6. Python基础_可迭代的/迭代器/生成器

    介绍 可迭代的:内部实现了__iter__方法 迭代器:内部实现了__iter__,__next__方法 生成器:yield,yield from 使用 __iter__() __next__() _ ...

  7. Eclipse各版本分析比较

    Eclipse最初是由IBM公司开发的替代商业软件Visual Age for Java的下一代IDE开发环境,2001年11月贡献给开源社区,现在它由非营利软件供应商联盟Eclipse基金会. Ec ...

  8. Scrum立会报告+燃尽图(Final阶段第四次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2481 项目地址:https://coding.net/u/wuyy694 ...

  9. 2-Sixth Scrum Meeting20151206

    任务分配 闫昊: 今日完成:请假.(最近代码写得多……很累……) 明日任务:完成数据库设计. 唐彬: 今日完成:ios客户端代码的深度学习. 明日任务:读IOS讨论区后台接口. 史烨轩: 今日完成:请 ...

  10. js实现把一个页面层数据传递到另一个页面

    由于之前面试,被问到过此问题,所以今天特意整理了一下.由于自己技术水平有限,若存在错误,欢迎提出批评. 本博客整理了两种方式从一个页面层向另一个页面层传递参数. 一. 通过cookie方式 1. 传递 ...