Intel Multi-Threading Contest
Thursday, November 22nd, 2007Meanwhile, I am in top 10 (winners) of Intel Multi-Threading Application Optimization Contest’ 07
Meanwhile, I am in top 10 (winners) of Intel Multi-Threading Application Optimization Contest’ 07
The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and then releasing the lock. This statement takes the following form:
lock(expression) statement_block
where:
expression
Specifies the object that you want to lock on. expression must be a reference type.
Typically, expression will either be this, if you want to protect an instance variable, or typeof(class), if you want to protect a static variable (or if the critical section occurs in a static method in the given class). statement_blockThe statements of the critical section.
lock(expression) statement_block is a shortcut for the following code snippet:
object x = expression;
System.Threading.Monitor.Enter(x);
try {
statement_block
}
finally {
System.Threading.Monitor.Exit(x);
}
Source: C# 3.0 Specification, MSDN
C# 3.0 Specification
The is operator is used to dynamically check if the run-time type of an object is compatible with a given type. The result of the operation E is T, where E is an expression and T is a type, is a boolean value indicating whether E can successfully be converted to type T by a reference conversion, a boxing conversion, or an unboxing conversion. The operation is evaluated as follows, after type arguments have been substituted for all type parameters:
Note that user defined conversions, are not considered by the is operator.
C# 3.0 Specification
The as operator is used to explicitly convert a value to a given reference type or nullable type. Unlike a cast expression, the as operator never throws an exception. Instead, if the indicated conversion is not possible, the resulting value is null.
In an operation of the form E as T, E must be an expression and T must be a reference type, a type parameter known to be a reference type, or a nullable type. Furthermore, at least one of the following must be true, or otherwise a compile-time error occurs:
The operation E as T produces the same result as
E is T ? (T)(E) : (T) null
except that E is only evaluated once. The compiler can be expected to optimize E as T to perform at most one dynamic type check as opposed to the two dynamic type checks implied by the expansion above.
Note that some conversions, such as user defined conversions, are not possible with the as operator and should instead be performed using cast expressions.
In the example
class X
{
public string F(object o) {
return o as string; // OK, string is a reference type
}
public T G<T>(object o) where T: Attribute {
return o as T;// Ok, T has a class constraint
}
public U H<U>(object o) {
return o as U; // Error, U is unconstrained
}
}
the type parameter T of G is known to be a reference type, because it has the class constraint. The type parameter U of H is not however; hence the use of the as operator in H is disallowed.
Keywords:
abstract as base bool break
byte case catch char checked
class const continue decimal default
delegate do double else enum
event explicit extern false finally
fixed float for foreach goto
if implicit in int interface
internal is lock long namespace
new null object operator out
override params private protected public
readonly ref return sbyte sealed
short sizeof stackalloc static string
struct switch this throw true
try typeof uint ulong unchecked
unsafe ushort using virtual void
volatile while yield
Each keyword will be discussed further…