Archive for November, 2007

Intel Multi-Threading Contest

Thursday, November 22nd, 2007

Meanwhile, I am in top 10 (winners) of Intel Multi-Threading Application Optimization Contest’ 07

Winners

C# Keywords: Lock

Tuesday, November 20th, 2007

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# Keywords : Is

Wednesday, November 14th, 2007

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:

  • If E is an anonymous function, a compile time error occurs
  • If E is a method group or the null literal, of if the type of E is a reference type or a nullable type and the value of E is null, the result is false.
  • Otherwise, let D represent the dynamic type of E as follows:
    • If the type of E is a reference type, D is the run-time type of the instance reference by E.
    • If the type of E is a nullable type, D is the underlying type of that nullable type.
    • If the type of E is a non-nullable value type, D is the type of E.
  • The result of the operation depends on D and T as follows:
    • If T is a reference type, the result is true if D and T are the same type, if D is a reference type and an implicit reference conversion from D to T exists, or if D is a value type and a boxing conversion from D to T exists.
    • If T is a nullable type, the result is true if D is the underlying type of T.
    • If T is a non-nullable value type, the result is true if D and T are the same type.
    • Otherwise, the result is false.

Note that user defined conversions, are not considered by the is operator.

C# Keywords : As

Wednesday, November 14th, 2007

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:

  • An identity , implicit reference , boxing , explicit reference , or unboxing conversion exists from the type of E to T.
  • The type of E or T is an open type.
  • E is the null literal.

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.

C# KeyWords

Tuesday, November 13th, 2007

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…

C# 3.0 Specification
C# 3.0 MSDN