c# IDictionary接口的应用

这是一个关于C#中实现IDictionary接口的示例,通过`SimpleDictionary`类展示了如何实现基本的字典操作,如添加、删除、查找元素等。文章详细解释了每个方法的实现逻辑,帮助理解IDictionary接口的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的实现有了大体甚至是全局的了解,看了以后会让你
茅塞顿开的,里面有些小细节与微软的方法还是有出入,实现一个方法有很多种,只要实现即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值