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类:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ConsoleApplication1
  7. {
  8. class CZuigao
  9. {
  10. //2个私有字段用来存储最高分学生的姓名和分数:
  11. private string name;
  12. private float score;
  13. //姓名和分数的属性:
  14. public string Name
  15. {
  16. get { return name; }
  17. set { if( value.Length > 1 ) { name = value; } }
  18. }
  19. public float Score
  20. {
  21. get { return score; }
  22. set { if( float.Parse(value.ToString()) > 0.1f ) { score = (float)value; } }
  23. }
  24. //显示存储在私有字段中的信息(姓名 分数);
  25. public void printf()
  26. {
  27. Console.WriteLine("最高分为:{1:f},最高分学生姓名是:{0}",name ,score );
  28. }
  29. //构造函数:
  30. public CZuigao() { }
  31. public CZuigao(string n, float f)
  32. {
  33. Name = n;
  34. Score = f;
  35. }
  36. public CZuigao(string nn)
  37. : this(nn, 0.11f) { }
  38. public CZuigao(float ff)
  39. : this("没有啊!", ff) { }
  40. }
  41. }

main():

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //调用3个内部构造函数:
  13. CZuigao cz = new CZuigao("李异峰");
  14. cz.printf();
  15. CZuigao cz1 = new CZuigao(2.3f);
  16. cz1.printf();
  17. CZuigao cz2 = new CZuigao("李晓峰", 3.6f);
  18. cz2.printf();
  19. //直接初始化赋值属性:
  20. CZuigao cz3 = new CZuigao() { Name = "我是属性!",Score=8.8f };
  21. cz3.printf();
  22. Console.WriteLine("下面是list<t>:");
  23. //list<T>:
  24. Program pro = new Program();
  25. List<CZuigao> lzg = new List<CZuigao>() {new CZuigao("list1",1.1f),new CZuigao("list2",2.1f),new CZuigao("list3",3.3f) };
  26. foreach( CZuigao scz in lzg ) {
  27. scz.printf();
  28. }
  29. Console.WriteLine("\n添加CZuigao对象后:");
  30. lzg.Add(cz );
  31. foreach( CZuigao scz in lzg ) {
  32. scz.printf();
  33. };
  34.  
  35. Console.ReadLine();
  36.  
  37. }
  38. }
  39.  
  40. }

F5:

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //调用3个内部构造函数:
  13. CZuigao cz = new CZuigao("李异峰");
  14. cz.printf();
  15. CZuigao cz1 = new CZuigao(2.3f);
  16. cz1.printf();
  17. CZuigao cz2 = new CZuigao("李晓峰", 3.6f);
  18. cz2.printf();
  19. //直接初始化赋值属性:
  20. CZuigao cz3 = new CZuigao() { Name = "我是属性!",Score=8.8f };
  21. cz3.printf();
  22. Console.WriteLine("\n下面是stack<t>:");
  23. //stack<T>:
  24. Stack<CZuigao> szg = new Stack<CZuigao>();
  25. szg.Push(new CZuigao("stack1", 9.8f));
  26. szg.Push(new CZuigao("stack2",9.2f));
  27. Console.WriteLine("最后输入的姓名:{0},分数是:{1}",szg.Peek().Name,szg.Peek().Score.ToString() );
  28. //添加CZuigao类的对象后:
  29. Console.WriteLine("\n下面是stack<t>添加CZuigao对象后的顶点:");
  30. szg.Push(cz );
  31. Console.WriteLine("顶点:姓名:{0},分数是:{1}", szg.Peek().Name, szg.Peek().Score.ToString());
  32. //移除2条后:
  33. szg.Pop();
  34. szg.Pop();
  35. Console.WriteLine("\n下面是stack<t>移除CZuigao的2条后的顶点:");
  36. Console.WriteLine("顶点:姓名:{0},分数是:{1}", szg.Peek().Name, szg.Peek().Score.ToString());
  37. //在移除后:
  38. try {
  39. szg.Pop();
  40. Console.WriteLine("顶点:姓名:{0},分数是:{1}", szg.Peek().Name, szg.Peek().Score.ToString());
  41. }
  42. catch(Exception e)
  43. {
  44. Console.WriteLine("\n\nszg里面为null,详情如下:{0}",e.ToString());
  45. }
  46. Console.ReadLine();
  47.  
  48. }
  49. }
  50.  
  51. }

F5:

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //调用3个内部构造函数:
  13. CZuigao cz = new CZuigao("李异峰");
  14. cz.printf();
  15. CZuigao cz1 = new CZuigao(2.3f);
  16. cz1.printf();
  17. CZuigao cz2 = new CZuigao("李晓峰", 3.6f);
  18. cz2.printf();
  19. //直接初始化赋值属性:
  20. CZuigao cz3 = new CZuigao() { Name = "我是属性!",Score=8.8f };
  21. cz3.printf();
  22. Console.WriteLine("\n下面是Queue<T>:");
  23. //Queue<T>:
  24. //初始化:
  25. Queue<CZuigao> qzg = new Queue<CZuigao>();
  26. qzg.Enqueue(new CZuigao("queue1", 5.1f));
  27. qzg.Enqueue(new CZuigao("queue2",5.2f));
  28. //返回顶点:
  29. Console.WriteLine("顶点上的姓名是:{0},分数是:{1:f}",qzg.Peek().Name ,qzg.Peek().Score);
  30. //添加上面CZuigao类的对象:
  31. qzg.Enqueue(cz );
  32. //返回顶点:
  33. Console.WriteLine("\n添加CZuIgao对象后:\n顶点上的姓名是:{0},分数是:{1:f}", qzg.Peek().Name, qzg.Peek().Score);
  34. //移除顶点:
  35. qzg.Dequeue();
  36. //返回顶点:
  37. Console.WriteLine("\n移除最高点后:\n顶点上的姓名是:{0},分数是:{1:f}", qzg.Peek().Name, qzg.Peek().Score);
  38. Console.ReadLine();
  39.  
  40. }
  41. }
  42.  
  43. }

F5:

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

  1. public class IComparse:IComparer <CZuigao >
  2. {
  3. #region IComparer<CZuigao> 成员
  4. int IComparer<CZuigao>.Compare(CZuigao x, CZuigao y)
  5. {
  6. if( x.Score > y.Score ) { return 1; }
  7. if( y.Score > x.Score ) { return -1; }
  8. else { return 0; }
  9. }
  10. #endregion
  11. }

再main():

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

F5后的数据比较:

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

  1. //移除最高点操作:
  2. sorzg.Remove((CZuigao)sorzg.Max );
  3. Console.WriteLine("\n当前sortedset内共有:{0:d}个对象数据;\n下面是移除最高点后的遍历:",sorzg.Count);
  4. 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>类似:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //using System.Collections;
  7. //using System.Collections.Specialized;
  8. using System.Collections.ObjectModel;
  9. namespace ConsoleApplication1
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. ObservableCollection<CZuigao> obzg = new ObservableCollection<CZuigao>() {new CZuigao ("observablecollection1",3.1f) };
  16.  
  17. obzg.CollectionChanged+=obzg_CollectionChanged;
  18. //添加对象:
  19. obzg.Add(new CZuigao("observablecollection2", 8.1f));
  20. //遍历当前所有的对象数据:
  21. Console.WriteLine("\n当前所有的对象数据:");
  22. foreach( CZuigao ss in obzg )
  23. {
  24. Console.WriteLine("name={0},\tscore={1:f}",ss.Name ,ss.Score );
  25. }
  26. Console.WriteLine("\n");
  27. //移除:
  28. obzg.RemoveAt();
  29. Console.ReadLine();
  30. }
  31. //public enum NotifyCollectionChangedAction
  32. //{
  33. // Add = 0,
  34. // Remove = 2,
  35. //}
  36. private static void obzg_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  37. {
  38. if( e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add )
  39. {
  40. Console.WriteLine("用户刚刚操作是添加操作,共添加了{0:d}个对象数据,详情如下:",e.NewItems.Count );
  41. foreach( CZuigao ss in e.NewItems )
  42. {
  43. Console.WriteLine("name={0},\tscore={1:f}",ss.Name,ss.Score );
  44. }
  45. }
  46. if( e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove )
  47. {
  48. Console.WriteLine("是移除操作,共移除{0:d}个对象数据,详情如下:",e.OldItems .Count );
  49. foreach( CZuigao ss in e.OldItems )
  50. {
  51. Console.WriteLine("name={0},\tscore={1:f}", ss.Name, ss.Score);
  52. }
  53. }
  54. }
  55.  
  56. }
  57.  
  58. }

F5:

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

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

main():

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

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

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

F5:

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

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

写一个generic的class:

  1. namespace ConsoleApplication1
  2. {
  3. class CValue<T>
  4. {
  5. public T values1, values2;
  6. public void printf()
  7. {
  8. Console.WriteLine("values1 is {0}\nvalues2 is {1}",values1.ToString(),values2.ToString());
  9. }
  10. }
  11. }

在main()中实现:

  1. static void Main(string[] args)
  2. {
  3. CValue<Int32> cv = new CValue<int>();
  4. cv.values1 = ;
  5. cv.values2 = ;
  6. cv.printf();
  7.  
  8. Console.ReadLine();
  9. }

=========================================================================================================
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):

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

添加个object约束:

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

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

  1. static void Main(string[] args)
  2. {
  3. , b = ;
  4. try { Cdiaohuan<Int32>(ref a, ref b); }
  5. catch( Exception ex ) { Console.WriteLine(ex.ToString()); }
  6.  
  7. Console.ReadLine();
  8. }

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

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

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. ipa包兼容性大作战!WeTest iOS深度兼容测试全新升级

    2018年,移动端适配话题热闹无比,有iOS新版本新机型发布,全面屏.异形屏.曲面屏争相斗艳,从而产生了各类特殊的屏幕分辨率设备. 正是因为这些特殊分辨率,导致2018年手机设备频繁出现适配问题,如屏 ...

  2. Linux之服务器介绍

    服务器,也称伺服器,是提供计算服务的设备.由于服务器需要响应服务请求,并进行处理,因此一般来说服务器应具备承担服务并且保障服务的能力. 服务器: 服务器指的是网络中能对其他机器提供某些服务的计算机系统 ...

  3. 【SIKIA计划】_07_Unity3D游戏开发-坦克大战笔记

    [新增分类][AudioClips]音频剪辑[AudioMixers]音频混合器[Editor][Fonts]字体[Materials]材质[Models]模型[Standard Assets] [渲 ...

  4. C#例题集

    收集一些从网上看到的例题 1.抽象类 抽象类不能被实例化一个抽象类只能通过接口和作为其它类的基类使用 抽象方法的声明只能在抽象类中 抽象方法必定不能实现(方法带一对{}都不行) 当一个子类集成自抽象类 ...

  5. VOT工具操作指南(踩过的坑)

    为了运行在VOT里DaSiamRPN,配置了很久环境,我电脑的配置是Ubuntu16.04+MatlabR2018a+pytorch0.3. 下面是一些从网上整理的操作步骤: 1.首先是工具箱的下载: ...

  6. Windows本地上传源码到Gitee远程仓库

    1.下载Git,并安装. 安装时一路默认即可 https://git-scm.com/downloads 验证Git安装成功否 cmd 下输入,出现版本号即成功 git --version 2.生成s ...

  7. rev命令详解

    基础命令学习目录首页 rev命令将文件中的每行内容以字符为单位反序输出,即第一个字符最后输出,最后一个字符最先输出,依次类推. #cat a.txt wo shi mcw, nihao how do ...

  8. mkfs命令详解

    mkfs命令-->make filesystem的缩写:用来在特定的分区建立Linux文件系统     [命令作用] 该命令用来在特定的分区创建linux文件系统,常见的文件系统有ext2,ex ...

  9. 详解HTTP缓存

    HTTP缓存是个大公司面试几乎必考的问题,写篇随笔说一下HTTP缓存. 1. HTTP报文首部中有关缓存的字段 在HTTP报文中,与缓存相关的信息都存在首部里,简单说一下首部. 首部 HTTP首部字段 ...

  10. 找"1"

    题目:给定一个十进制的正整数,写下从1开始,到N的所有整数,然后数一下其中出现“1”的次数. 要求:1.写一个函数f(N),返回1到N之间出现“1”的个数.例如f(12)=5. 2.在32位整数范围内 ...