Welcome

首页 / 软件开发 / .NET编程技术 / 揭示同步块索引(中):如何获得对象的HashCode

揭示同步块索引(中):如何获得对象的HashCode2011-10-15 博客园 横刀天笑Visual Studio + SOS 小实验

咋一看标题,觉得有些奇怪,同步块索引和HashCode有啥关系呢。从名字上来看离着十万八千里。在 不知道细节之前,我也是这样想的,知道细节之后,才发现这两兄弟如此亲密。我们还是先来用Visual Studio + SOS,看一个东西,下面是作为小白兔的示例代码:

1: using System;
2: public class Program
3: {
4: static void Main()
5: {
6: Foo f = new Foo();
7: Console.WriteLine(f.GetHashCode());
8:
9: Console.ReadLine();
10: }
11: }
12: //就这么一个简单的类
13: public class Foo
14: {
15:
16: }

(使用Visual Studio + SOS调试的时候,请先在项目的属性,调试栏里设置“允许非托管代码调试” )

我们分别在第7行,第9行设置断点,F5运行,当程序停在第一个断点处时(此时f.GetHashCode()还没 有执行),我们在Visual Studio的立即窗口里输入:

1: .load sos.dll
2: extension C:WindowsMicrosoft.NETFrameworkv2.0.50727sos.dll loaded
3: !dso
4: PDB symbol for mscorwks.dll not loaded
5: OS Thread Id: 0x1730 (5936)
6: ESP/REG Object Name
7: 0013ed78 01b72d58 Foo
8: 0013ed7c 01b72d58 Foo
9: 0013efc0 01b72d58 Foo
10: 0013efc4 01b72d58 Foo