当前位置:编程学习 > C#/ASP.NET >>

C#字符串留用机制与Lock.

 
因为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的时候最好不要用字符串,特别是拼接的字符,会没有效果.
我本来想利用这种拼接特性来完成不同级别的分层加锁,但经过测试没有效果.后面改用了其它方法才得以实现.
 
 
 
 
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,