因為C#的字符串留用機制,下面的代碼: [csharp] string theKey1 = "XXXXXX"; string theKey2 = "XXXXXX"; if (object.ReferenceEquals(theKey1, theKey2)) { string theC = theKey1 + theKey2; } string theKey1 = "XXXXXX"; string theKey2 = "XXXXXX"; if (object.ReferenceEquals(theKey1, theKey2)) { string theC = theKey1 + theKey2; }theKey1,theKey2指向的是同一個地址.但下面的代碼: [csharp] int theA = 1; string theKey1 = "XXX"+theA; string theKey2 = "XXX"+theA; if (object.ReferenceEquals(theKey1, theKey2)) { string theC = theKey1 + theKey2; } int theA = 1; string theKey1 = "XXX"+theA; string theKey2 = "XXX"+theA; if (object.ReferenceEquals(theKey1, theKey2)) { string theC = theKey1 + theKey2; } 中theKey1,theKey2引用是不相等的.說明C#的字符串留用機制僅針對字符串常量. 從上面的特性,其實Lock的時候最好不要用字符串,特別是拼接的字符,會沒有效果. 我本來想利用這種拼接特性來完成不同級別的分層加鎖,但經過測試沒有效果.後面改用了其它方法才得以實現.