浅析C# SortedList

开发 后端
本文介绍C# SortedList,对IDictionary进行操作是,发现用C# SortedList也可以等同于 ArryList和Hashtable的结合,只是通过Key排序。

对IDictionary进行操作是,发现用C# SortedList也可以等同于 ArryList和Hashtable的结合,只是通过Key排序,key不允许为空和null value可以,在效率上没有测试,但是保证是低,必定在插入时要比较排序。

C# SortedList通过公用方法得到信息:

  1. public IDictionary ExecuteDictionary( IDbCommand iCmd )  
  2. {  
  3. IDataReader reader = null;  
  4. try  
  5. {  
  6. //只读取一行数据,第一行  
  7. reader = iCmd.ExecuteReader( CommandBehavior.SingleRow );  
  8. }  
  9. catch(Exception e)  
  10. {  
  11. this.Close( iCmd );  
  12. return null;  
  13. }  
  14.  
  15. IDictionary dict = null;  
  16.  
  17. if(reader.Read())  
  18. {  
  19. int fieldCount = reader.FieldCount;  
  20. dict = new SortedList( fieldCount );  
  21.  
  22. for(int i = 0; i < fieldCount; i++)  
  23. {  
  24. dict [reader.GetName( i ).ToLower()] = reader.GetValue( i );  
  25. }  
  26. }  
  27.  
  28. reader.Close();  
  29. reader.Dispose();  
  30. return dict;  
  31. }  
  32.  
  33.    
  34.  
  35.  
  36. //返回list  
  37. public SortedList selectSingln()  
  38. {  
  39. DB.CommandText = @" SELECT TOP 5 * FROM products";  
  40. DB.CommandType = CommandType.Text;  
  41. return (SortedList)DB.ExecuteDictionary();  
  42. }  
  43.  
  44. //遍历list  
  45. private void _BeginRun()  
  46. {  
  47. _SqlServerLogic logic = new _SqlServerLogic();  
  48.  
  49. SortedList dic = logic.selectSingln();  
  50. Hashtable hash = new Hashtable();  
  51.  
  52. //遍历sortlist  
  53. foreach(DictionaryEntry entry in dic)  
  54. {  
  55. Response.Write( entry.Key + "***" + entry.Value + "<br>" );  
  56.  
  57. if( !string.IsNullOrEmpty( entry.Value.ToString() ) )  
  58. {  
  59. hash.Add( entry.Key, entry.Value );  
  60. }  
  61. }  
  62.  
  63. IDictionaryEnumerator item = hash.GetEnumerator();  
  64.  
  65. //遍历Hashtable  
  66. while( item.MoveNext() )  
  67. {  
  68. Response.Write( item.Key +"-----"+ item.Value +"<br/>" );  
  69. }  
  70.  
  71. string [] ary = new string [dic.Count];  
  72. dic.Keys.CopyTo( ary, 0 );  
  73.  
  74. Response.Write( string.Join( ",", ary ) );  
  75.  
  76. //for 遍历list  
  77. for(int i = 0; i < dic.Count; i++)  
  78. {  
  79. Response.Write( dic.GetKey(i)+"----"+ dic.GetByIndex(i) +"<br/>" );  
  80. }  

以上介绍C# SortedList

【编辑推荐】

  1. C#字符串进行分割
  2. 全面测试C#字符串
  3. C# out和ref传递数组
  4. 浅析C#定义整型数组
  5. C#数据库连接字符串
责任编辑:佚名 来源: MSDN
相关推荐

2009-08-17 18:34:50

C# ChangeCo

2009-08-14 17:45:52

C# ArrayLis

2009-08-25 17:59:49

C#入门

2009-08-27 13:30:11

C# interfac

2009-08-20 16:15:19

C# 匿名方法

2009-08-20 14:45:13

C# Switch语句

2009-08-21 17:24:06

C# SingleIn

2009-08-27 11:43:31

C#语法

2009-08-10 17:36:17

C#扩展方法

2009-08-26 13:07:07

C#交错数组

2009-08-18 09:24:52

C# Anonymou

2009-08-14 15:23:10

C#使用ErrorPr

2009-08-17 13:34:02

C#异步操作

2009-08-12 15:20:21

C#事件处理

2009-07-31 14:03:21

C# Format函数

2009-09-10 14:52:55

C# get

2009-08-18 10:30:30

C#枚举

2009-08-04 09:30:33

C#调用ImageAn

2009-08-21 17:24:06

C# SingleIn

2009-08-21 15:57:58

C# DataSour
点赞
收藏

51CTO技术栈公众号