2 <clause number="15.12" title="The lock statement">
3 <paragraph>The lock statement obtains the mutual-exclusion lock for a given object, executes a statement, and then releases the lock. <grammar_production><name><non_terminal where="15.12">lock-statement</non_terminal></name> : <rhs><keyword>lock</keyword><terminal>(</terminal><non_terminal where="14.14">expression</non_terminal><terminal>)</terminal><non_terminal where="15">embedded-statement</non_terminal></rhs></grammar_production></paragraph>
4 <paragraph>The expression of a lock statement must denote a value of a <non_terminal where="11.2">reference-type</non_terminal>. No implicit boxing conversion (<hyperlink>13.1.5</hyperlink>) is ever performed for the expression of a lock statement, and thus it is a compile-time error for the expression to denote a value of a <non_terminal where="11.1">value-type</non_terminal>. </paragraph>
5 <paragraph>A lock statement of the form <code_example><![CDATA[
7 ]]></code_example>where x is an expression of a <non_terminal where="11.2">reference-type</non_terminal>, is precisely equivalent to <code_example><![CDATA[
8 System.Threading.Monitor.Enter(x);
13 System.Threading.Monitor.Exit(x);
15 ]]></code_example>except that x is only evaluated once. </paragraph>
17 <example>[Example: The System.Type object of a class can conveniently be used as the mutual-exclusion lock for static methods of the class. For example: <code_example><![CDATA[
20 public static void Add(object x) {
21 lock (typeof(Cache)) {
25 public static void Remove(object x) {
26 lock (typeof(Cache)) {
31 ]]></code_example>end example]</example>