• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#创建安全的字典(Dictionary)存储结构

C#创建安全的字典(Dictionary)存储结构

作者:彭泽0902 字体:[增加 减小] 来源:互联网 时间:2017-05-28

彭泽0902 通过本文主要向大家介绍了c#dictionary用法,c#dictionary,c#遍历dictionary,c#中dictionary的用法,c#中dictionary等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

在上面介绍过栈(Stack)的存储结构,接下来介绍另一种存储结构字典(Dictionary)。 字典(Dictionary)里面的每一个元素都是一个键值对(由二个元素组成:键和值) 键必须是唯一的,而值不需要唯一的,键和值都可以是任何类型。字典(Dictionary)是常用于查找和排序的列表。

  接下来看一下Dictionary的部分方法和类的底层实现代码:

  1.Add:将指定的键和值添加到字典中。

  public void Add(TKey key, TValue value) {
      Insert(key, value, true); 
    }
 private void Insert(TKey key, TValue value, bool add) {
       if( key == null ) { 
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
      } 
      if (buckets == null) Initialize(0);
      int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF;
      int targetBucket = hashCode % buckets.Length; 
#if FEATURE_RANDOMIZED_STRING_HASHING 
      int collisionCount = 0; 
#endif
      for (int i = buckets[targetBucket]; i >= 0; i = entries[i].next) {
        if (entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key)) {
          if (add) {
            ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_AddingDuplicate); 
          }
          entries[i].value = value; 
          version++; 
          return;
        } 
#if FEATURE_RANDOMIZED_STRING_HASHING
        collisionCount++;
#endif 
      }
      int index; 
      if (freeCount > 0) { 
        index = freeList;
        freeList = entries[index].next; 
        freeCount--;
      }
      else {
        if (count == entries.Length) 
        {
          Resize(); 
          targetBucket = hashCode % buckets.Length; 
        }
        index = count; 
        count++;
      }
      entries[index].hashCode = hashCode; 
      entries[index].next = buckets[targetBucket];
      entries[index].key = key; 
      entries[index].value = value; 
      buckets[targetBucket] = index;
      version++; 
#if FEATURE_RANDOMIZED_STRING_HASHING
      if(collisionCount > HashHelpers.HashCollisionThreshold && HashHelpers.IsWellKnownEqualityComparer(comparer))
      { 
        comparer = (IEqualityComparer<TKey>) HashHelpers.GetRandomizedEqualityComparer(comparer);
        Resize(entries.Length, true); 
      } 
#endif
    }
</div>

  2.Clear():从 Dictionary<TKey, TValue> 中移除所有的键和值。

 public void Clear() {
      if (count > 0) {
        for (int i = 0; i < buckets.Length; i++) buckets[i] = -1;
        Array.Clear(entries, 0, count); 
        freeList = -1;
        count = 0; 
        freeCount = 0; 
        version++;
      } 
    }
</div>

   3.Remove():从 Dictionary<TKey, TValue> 中移除所指定的键的值。

 public bool Remove(TKey key) {
      if(key == null) {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
      } 
      if (buckets != null) { 
        int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF; 
        int bucket = hashCode % buckets.Length;
        int last = -1; 
        for (int i = buckets[bucket]; i >= 0; last = i, i = entries[i].next) {
          if (entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key)) {
            if (last < 0) {
              buckets[bucket] = entries[i].next; 
            }
            else { 
              entries[last].next = entries[i].next; 
            }
            entries[i].hashCode = -1; 
            entries[i].next = freeList;
            entries[i].key = default(TKey);
            entries[i].value = default(TValue);
            freeList = i; 
            freeCount++;
            version++; 
            return true; 
          }
        } 
      }
      return false;
    }
</div>

  4.GetEnumerator():返回循环访问 Dictionary<TKey, TValue> 的枚举器。

  public Enumerator GetEnumerator() {
      return new Enumerator(this, Enumerator.KeyValuePair); 
    }
 [Serializable] 
    public struct Enumerator: IEnumerator<KeyValuePair<TKey,TValue>>,
      IDictionaryEnumerator 
    { 
      private Dictionary<TKey,TValue> dictionary;
      private int version; 
      private int index;
      private KeyValuePair<TKey,TValue> current;
      private int getEnumeratorRetType; // What should Enumerator.Current return?
      internal const int DictEntry = 1;
      internal const int KeyValuePair = 2; 
      internal Enumerator(Dictionary<TKey,TValue> dictionary, int getEnumeratorRetType) {
        this.dictionary = dictionary; 
        version = dictionary.version;
        index = 0;
        this.getEnumeratorRetType = getEnumeratorRetType;
        current = new KeyValuePair<TKey, TValue>(); 
      }
      public bool MoveNext() { 
        if (version != dictionary.version) {
          ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); 
        }
        // Use unsigned comparison since we set index to dictionary.count+1 when the enumeration ends.
        // dictionary.count+1 could be negative if dictionary.count is Int32.MaxValue 
        while ((uint)index < (uint)dictionary.count) {
          if (dictionary.entries[index].hashCode >= 0) { 
            current = new KeyValuePair<TKey, TValue>(dictionary.entries[index].key, dictionary.entries[index].value); 
            index++;
            return true; 
          }
          index++;
        }
        index = dictionary.count + 1;
        current = new KeyValuePair<TKey, TValue>(); 
        return false; 
      }
      public KeyValuePair<TKey,TValue> Current {
        get { return current; }
      }
      public void Dispose() {
      } 
      object IEnumerator.Current {
        get { 
          if( index == 0 || (index == dictionary.count + 1)) {
            ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
          }
          if (getEnumeratorRetType == DictEntry) {
            return new System.Collections.DictionaryEntry(current.Key, current.Value); 
          } else { 
            return new KeyValuePair<TKey, TValue>(current.Key, current.Value);
          } 
        }
      }
      void IEnumerator.Reset() { 
        if (version != dictionary.version) {
          ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); 
        } 
        index = 0; 
        current = new KeyValuePair<TKey, TValue>();
      }
      DictionaryEntry IDictionaryEnumerator.Entry { 
        get {
          if( index == 0 || (index == dictionary.count + 1)) { 
             ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen); 
          }
          return new DictionaryEntry(current.Key, current.Value);
        }
      }
      object IDictionaryEnumerator.Key {
        get { 
          if( index == 0 || (index == dictionary.count + 1)) { 
             ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen);
          } 
          return current.Key;
        }
      } 
      object IDictionaryEnumerator.Value { 
        get { 
          if( index == 0 || (index == dictionary.count + 1)) {
             ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumOpCantHappen); 
          }
          return current.Value;
        } 
      }
    }
</div>

     上面主要是对字典(Dictionary)的一些常用方法进行一个简单的说明。接下来主要阐述如何创建安全的字典(Dictionary)存储结构。有关线程安全的部分,在这里就不再赘

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#创建安全的字典(Dictionary)存储结构
  • C#中Dynamic和Dictionary性能比较
  • C#针对xml文件转化Dictionary的方法
  • C#中Dictionary的作用及用法讲解
  • C#中查找Dictionary中重复值的方法
  • C# Dictionary的使用实例代码

相关文章

  • 2017-05-28在C#中新手易犯的典型缺陷
  • 2017-05-28C#学习笔记——基本语法
  • 2017-05-28C#实现的JS操作类实例
  • 2017-05-28DevExpress实现为TextEdit设置水印文字的方法
  • 2017-05-28C#中前台线程和后台线程的区别与联系
  • 2017-05-28c#对xml增删改查操作示例
  • 2017-05-28C#计算汽车行驶方向的方法分析
  • 2017-05-28c#实现隐藏与显示任务栏的方法详解
  • 2017-05-28Dictionary扩展基础类向字典中添加键和值
  • 2017-05-28C#零基础学习理解委托

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C#的3DES加密解密算法实例代码
    • c#生成随机数示例分享
    • C# IDE VS2005中的Hosting Process (vshost.exe)作用介绍
    • C#中设计、使用Fluent API
    • C#:(变量)字段和局部变量的作用域冲突
    • C# Base 64 编码/解码实现代码
    • C#运算符重载用法实例分析
    • C# yield在WCF中的错误使用(二)
    • c#遍历System.drawing.Color下面的所有颜色以及名称以查看
    • c# 以二进制读取文本文件

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有