c# IDictionary接口的應用


using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace fanxing
{

    
public class SimpleDictionary : IDictionary
    
{
        
private DictionaryEntry[] items;
        
private Int32 ItemsInUse = 0;

        
public SimpleDictionary(Int32 numItems)
        
{
            items 
= new DictionaryEntry[numItems];
        }


        
public bool IsReadOnly get return false; } }
        
public bool Contains(object key)
        
{
            Int32 index;
            
return TryGetIndexOfKey(key, out index);
        }

        
public bool IsFixedSize get return false; } }
        
public void Remove(object key)
        
{
            
if (key == nullthrow new ArgumentNullException("key");
            Int32 index;
            
if (TryGetIndexOfKey(key, out index))
            
{
                Array.Copy(items, index 
+ 1, items, index, ItemsInUse - index - 1);
                ItemsInUse
--;
            }

            
else
            
{

            }

        }

        
public void Clear()
        
{
            
if(ItemsInUse!=0)
            
{
                
for (int i = 0; i < ItemsInUse;i++ )
                
{
                    items[i].Key 
= null;
                    items[
9].Value = null;
                }

            }

            ItemsInUse 
= 0
        }

        
public void Add(object key, object value)
        
{
            
try
            
{
                
if (ItemsInUse == items.Length)
                    
throw new InvalidOperationException("The dictionary cannot hold any more items.");
                items[ItemsInUse
++= new DictionaryEntry(key, value);
            }

            
catch (Exception e)
            
{
                Console.Write(e.Message);
            }

        }

        
public ICollection Keys
        
{
            
get
            
{
                Object[] keys 
= new Object[ItemsInUse];
                
for (Int32 n = 0; n < ItemsInUse; n++)
                    keys[n] 
= items[n].Key;
                
return keys;
            }

        }

        
public ICollection Values
        
{
            
get
            
{
                Object[] values 
= new Object[ItemsInUse];
                
for (Int32 n = 0; n < ItemsInUse; n++)
                    values[n] 
= items[n].Value;
                
return values;
            }

        }

        
public object this[object key]
        
{
            
get
            
{
                Int32 index;
                
if (TryGetIndexOfKey(key, out index))
                
{
                    
return items[index].Value;
                }

                
else
                
{
                    
return null;
                }

            }


            
set
            
{
                Int32 index;
                
if (TryGetIndexOfKey(key, out index))
                
{
                    items[index].Value 
= value;
                }

                
else
                
{
                    Add(key, value);
                }

            }

        }

        
private Boolean TryGetIndexOfKey(Object key, out Int32 index)
        
{
            
for (index = 0; index < ItemsInUse; index++)
            
{
                
if (items[index].Key.Equals(key)) return true;
            }

            
return false;
        }

        
private class SimpleDictionaryEnumerator : IDictionaryEnumerator
        
{
            DictionaryEntry[] items;
            Int32 index 
= -1;

            
public SimpleDictionaryEnumerator(SimpleDictionary sd)
            
{
                items 
= new DictionaryEntry[sd.Count];
                Array.Copy(sd.items, 
0, items, 0, sd.Count);
            }


            
public Object Current get { ValidateIndex(); return items[index]; } }

            
public DictionaryEntry Entry
            
{
                
get return (DictionaryEntry)Current; }
            }


            
public Object Key get { ValidateIndex(); return items[index].Key; } }

            
public Object Value get { ValidateIndex(); return items[index].Value; } }

            
public Boolean MoveNext()
            
{
                
if (index < items.Length - 1{ index++return true; }
                
return false;
            }


            
private void ValidateIndex()
            
{
                
if (index < 0 || index >= items.Length)
                    
throw new InvalidOperationException("Enumerator is before or after the collection.");
            }


            
public void Reset()
            
{
                index 
= -1;
            }

        }

        
public IDictionaryEnumerator GetEnumerator()
        
{
            
return new SimpleDictionaryEnumerator(this);//這里需要注意,返回的是一個接口,那我們new的類即
SimpleDictionaryEnumerator就必須要繼承該接口,從而返回的該對象new SimpleDictionaryEnumerator(this)實例只能用IDictionaryEnumerator 接口下定義的方法。
        }

        
public bool IsSynchronized get return false; } }
        
public object SyncRoot get throw new NotImplementedException(); } }
        
public int Count get return ItemsInUse; } }
        
public void CopyTo(Array array, int index) throw new NotImplementedException(); }

        IEnumerator IEnumerable.GetEnumerator()
        
{
            
return ((IDictionary)this).GetEnumerator();
        }


       
class Application7
       
{

          
static void Main(string[] args)
           
{
               SimpleDictionary aa 
= new SimpleDictionary(2);
               aa.Add(
"a""aaa");
               aa.Add(
"b""aaa");
               Console.Write(aa.Keys.Count
+"===="+aa.Values.Count);
               IEnumerable bb 
= aa;
               bb.GetEnumerator();
              
foreach(DictionaryEntry cc in aa)
              
{
                 Console.Write(cc.Key);
              }

               Console.Read();
           }

       }

    }

}

 
這是我抄自msdn的例子:
IDictionary接口是hashtable實現的重要接口,對於復雜的hashtable大家可能感覺過於麻煩,東西太多,一頭霧水,因為hashtable里面有許多東西實現起來卻是麻煩,許多數據結構的知識在里面,容易讓人郁悶。但是看了 SimpleDictionary 這個類的實現,大家就可以對hashtable的實現有了大體甚至是全局的了解,看了以后會讓你
茅塞頓開的,里面有些小細節與微軟的方法還是有出入,實現一個方法有很多種,只要實現即可。

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com