淺析C# SortedList
作者:佚名
本文介紹C# SortedList,對(duì)IDictionary進(jìn)行操作是,發(fā)現(xiàn)用C# SortedList也可以等同于 ArryList和Hashtable的結(jié)合,只是通過(guò)Key排序。
對(duì)IDictionary進(jìn)行操作是,發(fā)現(xiàn)用C# SortedList也可以等同于 ArryList和Hashtable的結(jié)合,只是通過(guò)Key排序,key不允許為空和null value可以,在效率上沒(méi)有測(cè)試,但是保證是低,必定在插入時(shí)要比較排序。
C# SortedList通過(guò)公用方法得到信息:
- public IDictionary ExecuteDictionary( IDbCommand iCmd )
- {
- IDataReader reader = null;
- try
- {
- //只讀取一行數(shù)據(jù),第一行
- reader = iCmd.ExecuteReader( CommandBehavior.SingleRow );
- }
- catch(Exception e)
- {
- this.Close( iCmd );
- return null;
- }
- IDictionary dict = null;
- if(reader.Read())
- {
- int fieldCount = reader.FieldCount;
- dict = new SortedList( fieldCount );
- for(int i = 0; i < fieldCount; i++)
- {
- dict [reader.GetName( i ).ToLower()] = reader.GetValue( i );
- }
- }
- reader.Close();
- reader.Dispose();
- return dict;
- }
- //返回list
- public SortedList selectSingln()
- {
- DB.CommandText = @" SELECT TOP 5 * FROM products";
- DB.CommandType = CommandType.Text;
- return (SortedList)DB.ExecuteDictionary();
- }
- //遍歷list
- private void _BeginRun()
- {
- _SqlServerLogic logic = new _SqlServerLogic();
- SortedList dic = logic.selectSingln();
- Hashtable hash = new Hashtable();
- //遍歷sortlist
- foreach(DictionaryEntry entry in dic)
- {
- Response.Write( entry.Key + "***" + entry.Value + "<br>" );
- if( !string.IsNullOrEmpty( entry.Value.ToString() ) )
- {
- hash.Add( entry.Key, entry.Value );
- }
- }
- IDictionaryEnumerator item = hash.GetEnumerator();
- //遍歷Hashtable
- while( item.MoveNext() )
- {
- Response.Write( item.Key +"-----"+ item.Value +"<br/>" );
- }
- string [] ary = new string [dic.Count];
- dic.Keys.CopyTo( ary, 0 );
- Response.Write( string.Join( ",", ary ) );
- //for 遍歷list
- for(int i = 0; i < dic.Count; i++)
- {
- Response.Write( dic.GetKey(i)+"----"+ dic.GetByIndex(i) +"<br/>" );
- }
- }
以上介紹C# SortedList
【編輯推薦】
責(zé)任編輯:佚名
來(lái)源:
MSDN