Reback 2011.10.25

本文探讨了C#中Dictionary的工作原理,包括添加键值对时如何比较键以及使用Foreach遍历列表的内部机制。通过ILSpy反编译工具揭示了.NET框架底层实现细节。

Since i forget my password, i can't login in nearly one month. Why so long ?  because in company,  other mail  box is forbidden. so i should use my homenetwork, but  i has no network at home.

lately, i learn c# now.colleague  advie a tool named ILSPY, it's a decompile tool, can see some code about how to  complete core function.  it's very useful. the  address url as follow: http://wiki.sharpdevelop.net/ILSpy.ashx

here give two example:

first is  about Dictionary

When we  add a new   pair  <key, value> to a  Dictionary , what will be  compare  with key   ?

 

ILSpy ‘s   result  and   my   simple  comment , any question  link me.

 

//hash code

int num = this.comparer.GetHashCode(key) & 2147483647;

int num2 = num % this.buckets.Length;

for (int i = this.buckets[num2]; i >= 0; i = this.entries[i].next)
    {

// first judge  hashcode ,  then   use  equals  judge,  as default ,  equals  use  RefferenceEquals, just  the address  of   memory. So it is 
        if (this.entries[i].hashCode == num && this.comparer.Equals(this.entries[i].key, key))
        {
            if (add)
            {
                ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_AddingDuplicate);
            }
            this.entries[i].value = value;
            this.version++;
            return;
        }
    }

/////////////////////

second is Foreach

private bool MoveNextRare()

{

//use foreach, version is a key step

 if (this.version != this.list._version)

 {

ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);

}

 this.index = this.list._size + 1;

this.current = default(T);

return false;

 }

 

转载于:https://www.cnblogs.com/cquccy/archive/2011/10/26/2225037.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值