lock下面的示例包含一个lock

class Account
{
decimal balance;
private Object thisLock = new Object();

public void Withdraw(decimal amount)
{
lock (thisLock)
{
if (amount > balance)
{
throw new Exception("Insufficient funds");
}
balance -= amount;
}
}
}



lock 如果其他线程尝试进入锁定的代码,则它将一直等待(即被阻止),直到该对象被释放


提供给 lock在上面的示例中,锁的范围限定为此函数,因为函数外不存在任何对对象lockThis如果确实存在此类引用,锁的范围将扩展到该对象。严格地说,提供的对象只是用来唯一地标识由多个线程共享的资源,所以它可以是任意类实例。然而,实际上,此对象通常表示需要进行线程同步的资源。例如,如果一个容器对象将被多个线程使用,则可以将该容器传递给 lock,而 lock 后面的同步代码块将访问该容器。只要其他线程在访问该容器前先锁定该容器,则对该对象的访问将是安全同步的。

通常,最好避免锁定 public例如,如果该实例可以被公开访问,则lock(this)这可能导致死锁,即两个或更多个线程等待释放同一对象。出于同样的原因,锁定公共数据类型(相比于对象)也可能导致问题。锁定字符串尤其危险,因为字符串被公共语言运行时 (CLR)“暂留”。这意味着整个程序中任何给定字符串都只有一个实例,就是这同一个对象表示了所有运行的应用程序域的所有线程中的该文本。因此,只要在应用程序进程中的任何位置处具有相同内容的字符串上放置了锁,就将锁定应用程序中该字符串的所有实例。因此,最好锁定不会被暂留的私有或受保护成员。

lock 关键字在块的开始处调用​​Enter​​​,而在块的结尾处调用​​Exit​​​。​​ThreadInterruptedException​​​ 引发,如果 ​​Interrupt​​ 中断等待输入lock

通常,应避免锁定 public常见的结构lock (this)、lock (typeof (MyType)) 和lock ("myLock")

  • 如果实例可以被公共访问,将出现 lock (this)
  • 如果 MyType 可以被公共访问,将出现lock (typeof (MyType))
  • 由于进程中使用同一字符串的任何其他代码都将共享同一个锁,所以出现 lock("myLock")

最佳做法是定义 private 对象来锁定, 或private static

在 lock




可以使用 as 运算符执行转换的某些类型在兼容之间的引用类型或​​可以为 null 的类型​​。下面的代码提供了一个示例。


C#

class csrefKeywordsOperators
{
class Base
{
public override string ToString()
{
return "Base";
}
}
class Derived : Base
{ }

class Program
{
static void Main()
{

Derived d = new Derived();

Base b = d as Base;
if (b != null)
{
Console.WriteLine(b.ToString());
}

}
}
}


备注




as但是,因此,如果转换是不可能的,as 返回null请看下面的示例:




​复制​



expression as type



代码与下面的表达式是等效的,但 expression



复制



expression is type ? (type)expression : (type)null



请注意 asas


示例




C#



复制



class ClassA { }
class ClassB { }

class MainClass
{
static void Main()
{
object[] objArray = new object[6];
objArray[0] = new ClassA();
objArray[1] = new ClassB();
objArray[2] = "hello";
objArray[3] = 123;
objArray[4] = 123.4;
objArray[5] = null;

for (int i = 0; i < objArray.Length; ++i)
{
string s = objArray[i] as string;
Console.Write("{0}:", i);
if (s != null)
{
Console.WriteLine("'" + s + "'");
}
else
{
Console.WriteLine("not a string");
}
}
}
}
/*
Output:
0:not a string
1:not a string
2:'hello'
3:not a string
4:not a string
5:not a string
*/