Monday, January 28, 2013

TD Bank Phone service

Chinese telephone service (until 12:00 pm)
Credit Card: 1 - 877 - 233 - 5844
Debit Card: 1 - 800 - 387 -2828
Employee banking number (need call in the company):
1 - 888 -893 -3958

Sunday, January 27, 2013

Root and upgrade Samsung GT-N7000


current version 2.3.6
1. Root
1.1 Download Odin307.zip and AbyssNotekernel42CWTouchOriginalLogo.tar.
1.2 Run Odin, should see ID:COM, select PDA and the tar, click start.
1.3 verify it's rooted.
http://rootgalaxynote.com/galaxy-note-root/how-to-root-galaxy-note-gt-n7000-method-2odinheimdall/
2. upgrade
install rom manager 5.8.1.5 through wandoujia (android app), download the rom JellyBeen_eybee1970.zip
open rom manager, select backup and restore, not supported.
http://www.youtube.com/watch?v=M0dY00Re2ac
restore to facotry and connect to play store, install latest rom manager 6.0 and select "install ROM from SD card"

Monday, January 14, 2013

准备看牙,牙医相关

http://forum.iask.bz/showthread.php?t=142332

http://forum.iask.bz/showthread.php?t=520264

Tuesday, January 8, 2013

CNH and NDC

Since currency flows in and out of mainland China are still restricted, RMB traded on the Hong Kong market (known as CNH) can have a different value to RMB traded on the mainland. Other RMB markets include the dollar settled non-deliverable forward (NDF) and the trade-settlement exchange rate (CNT).

Monday, January 7, 2013

Tom and Stephen Feuerstein have different opinion on the exception handling

http://tkyte.blogspot.ca/2008/01/why-do-people-do-this.html

Tom and Stephen Feuerstein have different opinion on the exception handling

Saturday, January 5, 2013

Value Types Versus Reference Types
All C# types fall into the following categories:
? Value types
? Reference types
? Generic type parameters
? Pointer types
Value types comprise most built-in types (specifically, all numeric types, the char
type, and the bool type) as well as custom struct and enum types.
Reference types comprise all class, array, delegate, and interface types.
The fundamental difference between value types and reference types is how they are
handled in memory.
The content of a value type variable or constant is simply a value.
The assignment of a value type instance always copies the instance.
The content of a reference-type variable or constant is
a reference to an object that contains the value.
Assigning a reference-type variable copies the reference, not the object instance.
A reference can be assigned the literal null, indicating that the reference points to
no object.
In contrast, a value type cannot ordinarily have a null value.
Value-type instances occupy precisely the memory required to store their fields.
The System.IntPtr and System.UIntPtr types are also primitive.
The decimal type is typically used for financial
calculations, where base-10-accurate arithmetic and high precision are
required. It's non-native to processor (about 10 times slower than
double.
The stack and the heap are the places where variables and constants reside. Each
has very different lifetime semantics.
The stack is a block of memory for storing local variables and parameters. The stack
logically grows and shrinks as a function is entered and exited.
The heap is a block of memory in which objects (i.e., reference-type instances) reside.
Whenever a new object is created, it is allocated on the heap, and a reference to that
object is returned. During a program’s execution, the heap starts filling up as new
objects are created. The runtime has a garbage collector that periodically deallocates
objects from the heap, so your computer does not run out of memory. An object is
eligible for deallocation as soon as nothing references it.
Value-type instances (and object references) live wherever the variable was declared.
If the instance was declared as a field within an object, or as an array element, that
instance lives on the heap.
The heap also stores static fields and constants. Unlike objects allocated on the heap
(which can get garbage-collected), these live until the application domain is torn
down.
C# enforces a definite assignment policy. In practice, this means that outside of an
unsafe context, it’s impossible to access uninitialized memory. Definite assignment
has three implications:
? Local variables must be assigned a value before they can be read.
? Function arguments must be supplied when a method is called.
? All other variables (such as fields and array elements) are automatically initialized
by the runtime.
Fields and array elements are automatically initialized with the default values for
their type.
decimal d = default (decimal);
Parameter modifier Passed by Variable must be definitely assigned
None Value Going in
ref Reference Going in
out Reference Going out
An out argument is like a ref argument, except it:
? Need not be assigned before going into the function
? Must be assigned before it comes out of the function
The out modifier is most commonly used to get multiple return values back from a
method.
Passing a reference-type argument by value copies the reference, but not the object.
The params parameter modifier may be specified on the last parameter of a method
so that the method accepts any number of parameters of a particular type. The
parameter type must be declared as an array.
From C# 4.0, methods, constructors, and indexers can declare optional
parameters. Optional parameters cannot be
marked with ref or out.
Rather than identifying an argument by position, you can identify an argument by
name.
A constant declaration is like a variable declaration, except that the variable cannot
be changed after it has been declared, and the initialization must occur with the
declaration.
A field is a variable that is a member of a class or struct.
The readonly modifier prevents a field from being modified after construction.
Properties look like fields from the outside, but internally they contain logic, like
methods do.
A property is declared like a field, but with a get/set block added. Here’s how to
implement CurrentPrice as a property.
To write an indexer, define a property called this, specifying the arguments in square
brackets.
A constant is much more restrictive than a static readonly field—both in the types
you can use and in field initialization semantics. A constant also differs from a static
readonly field in that the evaluation of the constant occurs at compile time.
It makes sense for PI to be a constant, since it can never change. In contrast, a static
readonly field can have a different value per application.
A static constructor executes once per type, rather than once per instance. A type
can define only one static constructor, and it must be parameterless and have the
same name as the type.
Static field initializers run just before the static constructor is called. If a type has no
static constructor, field initializers will execute just prior to the type being used—
or anytime earlier at the whim of the runtime. (This means that the presence of a
static constructor may cause field initializers to execute later in the program than
they would otherwise.)
Static field initializers run in the order in which the fields are declared.
A class can be marked static, indicating that it must be composed solely of static
members and cannot be subclassed. The System.Console and System.Math classes are
good examples of static classes.
A partial method consists of two parts: a definition and an implementation. Partial methods must
be void and are implicitly private.
The as operator performs a downcast that evaluates to null (rather than throwing
an exception) if the downcast fails.
The is operator tests whether a reference conversion would succeed; in other words,
whether an object derives from a specified class (or implements an interface). It is
often used to test before downcasting.
A function marked as virtual can be overridden by subclasses wanting to provide a
specialized implementation. Methods, properties, indexers, and events can all be
declared virtual.
Abstract classes are able to define abstract members. Abstract members are like virtual
members, except they don’t provide a default implementation. That implementation
must be provided by the subclass, unless that subclass is also declared
abstract.

A struct is similar to a class, with the following key differences:
? A struct is a value type, whereas a class is a reference type.
? A struct does not support inheritance (other than implicitly deriving from
object, or more precisely, System.ValueType).


Managed code is represented in Intermediate Language or IL. When the CLR loads
an assembly, it converts the IL into the native code of the machine, such as x86. This
conversion is done by the CLR’s JIT (Just-In-Time) compiler.

dynamic d = "hello";
Console.WriteLine (d.ToUpper()); // HELLO
Console.WriteLine (d.Foo()); // Compiles OK but gives runtime error

All C# types fall into the following categories:
? Value types
? Reference types
? Generic type parameters
? Pointer types
Value types comprise most built-in types (specifically, all numeric types, the char
type, and the bool type) as well as custom struct and enum types.
Reference types comprise all class, array, delegate, and interface types.
The fundamental difference between value types and reference types is how they are
handled in memory.
The content of a value type variable or constant is simply a value.
The assignment of a value type instance always copies the instance.
A value type cannot ordinarily have a null value.
Value-type instances occupy precisely the memory required to store their fields.
A reference type is more complex than a value type, having two parts: an object and
the reference to that object. The content of a reference-type variable or constant is
a reference to an object that contains the value.
Assigning a reference-type variable copies the reference, not the object instance.
Reference types require separate allocations of memory for the reference and object.
The object consumes as many bytes as its fields, plus additional administrative
overhead. The precise overhead is intrinsically private to the implementation of
the .NET runtime, but at minimum the overhead is eight bytes, used to store a key
to the object’s type, as well as temporary information such as its lock state for
multithreading and a flag to indicate whether it has been fixed from movement by
the garbage collector. Each reference to an object requires an extra 4 or 8 bytes,
depending on whether the .NET runtime is running on a 32- or 64-bit platform.

The set of predefined value types excluding decimal are known as primitive types in
the CLR. Primitive types are so called because they are supported directly via instructions
in compiled code, and this usually translates to direct support on the
underlying processor.
The System.IntPtr and System.UIntPtr types are also primitive.

When you cast from a floating-point number to an integral, any
fractional portion is truncated; no rounding is performed. The
static class System.Convert provides methods that round while
converting between various numeric types.

The checked operator tells the runtime to generate an OverflowException rather than
failing silently when an integral expression or statement exceeds the arithmetic limits
of that type. The counterpart is unchecked.

decimal is useful for financial computations and values that are “man-made” rather
than the result of real-world measurements. Internal representation is
Base 10. Non-native to processor (about 10 times slower than
double)

Although a Boolean value requires only one bit of storage, the runtime will use one
byte of memory, since this is the minimum chunk that the runtime and processor
can efficiently work with. To avoid space inefficiency in the case of arrays, the
Framework provides a BitArray class in the System.Collections namespace that is
designed to use just one bit per Boolean value.

For reference types, equality, by default, is based on reference, as opposed to the
actual value of the underlying object.

C#’s char type (aliasing the System.Char type) represents a Unicode character and
occupies two bytes.

string is a reference type, rather than a value type. Its equality
operators, however, follow value-type semantics.

Creating an array always preinitializes the elements with default values. The default
value for a type is the result of a bitwise zeroing of memory. An array itself is
always a reference type object, regardless of the element type.

The stack is a block of memory for storing local variables and parameters.
The heap is a block of memory in which objects (i.e., reference-type instances) reside.
Whenever a new object is created, it is allocated on the heap, and a reference to that
object is returned.If the instance was declared as a field within an object, or as an array element, that
instance lives on the heap.The heap also stores static fields and constants. Unlike objects allocated on the heap
(which can get garbage-collected), these live until the application domain is torn
down.
Fields and array elements are automatically initialized with the default values for
their type.
You can obtain the default value for any type using the default keyword.

By default, arguments in C# are passed by value, which is by far the most common
case. This means a copy of the value is created when passed to the method.
Passing a reference-type argument by value copies the reference, but not the object.
To pass by reference, C# provides the ref parameter modifier.
An out argument is like a ref argument, except it:
? Need not be assigned before going into the function
? Must be assigned before it comes out of the function
Like a ref parameter, an out parameter is passed by reference.

The params parameter modifier may be specified on the last parameter of a method
so that the method accepts any number of parameters of a particular type. The
parameter type must be declared as an array.
The default value of an optional parameter must be specified by a constant expression,
or a parameterless constructor of a value type. Optional parameters cannot be
marked with ref or out.
Mandatory parameters must occur before optional parameters in both the method
declaration and the method call (the exception is with params arguments, which still
always come last).
Rather than identifying an argument by position, you can identify an argument by
name.Named arguments can occur in any order.
positional parameters must come before named arguments.
So we couldn’t call Foo like this:
Foo (x:1, 2); // Compile-time error

A constant declaration is like a variable declaration, except that the variable cannot
be changed after it has been declared, and the initialization must occur with the
declaration.

A field is a variable that is a member of a class or struct.
A
read-only field can be assigned only in its declaration or within the enclosing type’s
constructor.
Field initializers run before constructors.
A method’s signature comprises
its name and parameter types (but not the parameter names, nor the return
type).
Whether a parameter is pass-by-value or pass-by-reference is also part of the signature.
For example, Foo(int) can coexist with either Foo(ref int) or Foo(out int).
However, Foo(ref int) and Foo(out int) cannot coexist.
You can pass an expression into another constructor as follows:
public Wine (decimal price, DateTime year) : this (price, year.Year) { }

For classes, the C# compiler automatically generates a parameterless constructor if
and only if you do not define any constructors. However, as soon as you define at
least one constructor, the parameterless constructor is no longer automatically
generated.
For structs, a parameterless constructor is intrinsic to the struct; therefore, you cannot
define your own. The role of a struct’s implicit parameterless constructor is to
initialize each field with default values.

Properties look like fields from the outside, but internally they contain logic, like
methods do.
To write an indexer, define a property called this, specifying the arguments in square
brackets.

A constant is a field whose value can never change. A constant is evaluated statically
at compile time and the compiler literally substitutes its value whenever used, rather
like a macro in C++.
A constant is much more restrictive than a static readonly field—both in the types
you can use and in field initialization semantics. A constant also differs from a static
readonly field in that the evaluation of the constant occurs at compile time.
It makes sense for PI to be a constant, since it can never change. In contrast, a static
readonly field can have a different value per application.

A type can define only one static constructor.

The runtime automatically invokes a static constructor just prior to the type being
used. Two things trigger this:
? Instantiating the type
? Accessing a static member in the type
If a static constructor throws an unhandled exception , that type
becomes unusable for the life of the application.

Static field initializers run just before the static constructor is called. If a type has no
static constructor, field initializers will execute just prior to the type being used—
or anytime earlier at the whim of the runtime. (This means that the presence of a
static constructor may cause field initializers to execute later in the program than
they would otherwise.)
Static field initializers run in the order in which the fields are declared.
class Program
{
static void Main() { Console.WriteLine (Foo.X); } // 3
}
class Foo
{
public static Foo Instance = new Foo();
public static int X = 3;
Foo() { Console.WriteLine (X); } // 0
}
//Same as Java

A class can be marked static, indicating that it must be composed solely of static
members and cannot be subclassed. The System.Console and System.Math classes are
good examples of static classes.

Finalizers are class-only methods that execute before the garbage collector reclaims
the memory for an unreferenced object.

A partial type may contain partial methods.
A partial method consists of two parts: a definition and an implementation. The
definition is typically written by a code generator, and the implementation is typically
manually authored. If an implementation is not provided, the definition of the
partial method is compiled away. This allows auto-generated code to be liberal in
providing hooks, without having to worry about code bloat. Partial methods must
be void and are implicitly private.

The as operator performs a downcast that evaluates to null (rather than throwing
an exception) if the downcast fails.
The is operator tests whether a reference conversion would succeed; in other words,
whether an object derives from a specified class (or implements an interface). It is
often used to test before downcasting.

A function marked as virtual can be overridden by subclasses wanting to provide a
specialized implementation. Methods, properties, indexers, and events can all be
declared virtual.
An overridden method can call its base class implementation via
the base keyword.
Abstract classes are able to define abstract members. Abstract members are like virtual
members, except they don’t provide a default implementation.

An overridden function member may seal its implementation with the sealed keyword
to prevent it from being overridden by further subclasses:
public sealed override decimal Liability { get { return Mortgage; } }

The base keyword is similar to the this keyword. It serves two essential purposes:
? Accessing an overridden function member from the subclass
? Calling a base class constructor (see the next section)

If a constructor in a subclass omits the base keyword, the base type’s parameterless
constructor is implicitly called. (same as Java)
If the base class has no parameterless constructor, subclasses are forced to use the
base keyword in their constructors.

When an object is instantiated, initialization takes place in the following order:
1. From subclass to base class:
a. Fields are initialized.
b. Arguments to base-class constructor calls are evaluated.
2. From base class to subclass:
a. Constructor bodies execute.

Boxing is the act of casting a value-type instance to a reference-type instance.

There are two basic ways to get a System.Type object:
? Call GetType on the instance.
? Use the typeof operator on a type name.
GetType is evaluated at runtime; typeof is evaluated statically at compile time.

A struct is similar to a class, with the following key differences:
? A struct is a value type, whereas a class is a reference type.
? A struct does not support inheritance (other than implicitly deriving from
object, or more precisely, System.ValueType).
A struct can have all the members a class can, except the following:
? A parameterless constructor
? A finalizer
? Virtual members
The construction semantics of a struct are as follows:
? A parameterless constructor that you can’t override implicitly exists. This performs
a bitwise-zeroing of its fields.
? When you define a struct constructor, you must explicitly assign every field.
? You can’t have field initializers in a struct.
public struct Point
{
int x = 1; // Illegal: cannot initialize field
int y;
public Point() {} // Illegal: cannot have
// parameterless constructor
public Point (int x) {this.x = x;} // Illegal: must assign field y
}

Class2 is accessible from outside its assembly; Class1 is not:
class Class1 {} // Class1 is internal (default)
public class Class2 {}
ClassB exposes field x to other types in the same assembly; ClassA does not:
class ClassA { int x; } // x is private (default)
class ClassB { internal int x; }
When overriding a base class function, accessibility must be identical on the overridden
function. (No in Java)
a subclass itself can be less accessible than a base class, but not more.

Structs can implement interfaces. In contrast, a struct cannot inherit from a
class.
An interface
can contain only methods, properties, events, and indexers.
Interface members are always implicitly public and cannot declare an access modifier.
Implementing an interface means providing a public implementation for all its
members.
Enum.IsDefined

A nested type has the following features:
? It can access the enclosing type’s private members and everything else the enclosing
type can access.
? It can be declared with the full range of access modifiers, rather than just
public and internal.
? The default visibility for a nested type is private rather than internal.
? Accessing a nested type from outside the enclosing type requires qualification
with the enclosing type’s name (like when accessing static members).
All types can be nested; however, only classes and structs can nest.

In the following example, GenericClass<T,U> requires T to derive from SomeClass and
implement Interface1, and requires U to provide a parameterless constructor:
class SomeClass {}
interface Interface1 {}
class GenericClass<T> where T : SomeClass, Interface1
where U : new()
{...}

Technically, all type arguments on a subtype are fresh: you
could say that a subtype closes and then reopens the base type
arguments. This means that a subclass can give new (and potentially
more meaningful) names to the type arguments it
reopens.
Static data is unique for each closed type:
Console.WriteLine (++Bob<int>.Count); // 1
Console.WriteLine (++Bob<int>.Count); // 2
Console.WriteLine (++Bob<string>.Count); // 1

Generic classes are not covariant, to ensure static type safety.
For historical reasons, array types are covariant. This means that B[] can be cast to
A[] if B subclasses A (and both are reference types). For example:
Bear[] bears = new Bear[3];
Animal[] animals = bears; // OK
The downside of this reusability is that element assignments can fail at runtime:
animals[0] = new Camel(); // Runtime error
As of C# 4.0, generic interfaces support covariance for type parameters marked with
the out modifier.
Curiously, method parameters marked as out are not eligible for
covariance, due to a limitation in the CLR.

C# generics are similar in application to C++ templates, but they work very differently.
However,
with C# generics, producer types (i.e., open types such as List<T>) can be compiled
into a library (such as mscorlib.dll). This works because the synthesis between the
producer and the consumer that produces closed types doesn’t actually happen until
runtime. With C++ templates, this synthesis is performed at compile time. This
means that in C++ you don’t deploy template libraries as .dlls—they exist only as
source code. It also makes it difficult to dynamically inspect, let alone create, parameterized
types on the fly.

A delegate dynamically wires up a method caller to its target method. There are two
aspects to a delegate: type and instance. A delegate type defines a protocol to which
the caller and target will conform, comprising a list of parameter types and a return
type. A delegate instance is an object that refers to one (or more) target methods
conforming to that protocol.
A delegate instance literally acts as a delegate for the caller: the caller invokes the
delegate, and then the delegate calls the target method. This indirection decouples
the caller from the target method.

A delegate is similar to a callback, a general term that captures
constructs such as C function pointers.

All delegate instances have multicast capability. This means that a delegate instance
can reference not just a single target method, but also a list of target methods. The
+ and += operators combine delegate instances. For example:
SomeDelegate d = SomeMethod1;
d += SomeMethod2;
The last line is functionally the same as:
d = d + SomeMethod2;
Invoking d will now call both SomeMethod1 and SomeMethod2. Delegates are invoked
in the order they are added.

If a multicast delegate has a nonvoid return type, the caller receives the return value
from the last method to be invoked. The preceding methods are still called, but their
return values are discarded. In most scenarios in which multicast delegates are used,
they have void return types, so this subtlety does not arise.

A delegate type may contain generic type parameters. For example:
public delegate T Transformer<T> (T arg);
With this definition, we can write a generalized Transform utility method that works
on any type.
A problem that can be solved with a delegate can also be solved with an
interface.
A delegate design may be a better choice than an interface design if one or more of
these conditions are true:
? The interface defines only a single method.
? Multicast capability is needed.
? The subscriber needs to implement the interface multiple times.

When you call a method, you can supply arguments that have more specific types
than the parameters of that method. This is ordinary polymorphic behavior. For
exactly the same reason, a delegate can have more specific parameter types than its
method target. This is called contravariance.

If you call a method, you may get back a type that is more specific than what you
asked for. This is ordinary polymorphic behavior. For exactly the same reason, the
return type of a delegate can be less specific than the return type of its target method.
This is called covariance.

When using delegates, two emergent roles commonly appear: broadcaster and
subscriber.
The broadcaster is a type that contains a delegate field. The broadcaster decides
when to broadcast, by invoking the delegate.
The subscribers are the method target recipients. A subscriber decides when to start
and stop listening, by calling += and ?= on the broadcaster’s delegate. A subscriber
does not know about, or interfere with, other subscribers.
Events are a language feature that formalizes this pattern. An event is a construct
that exposes just the subset of delegate features required for the broadcaster/
subscriber model. The main purpose of events is to prevent subscribers from interfering
with each other.
The easiest way to declare an event is to put the event keyword in front of a delegate
member:
public class Broadcaster
{
public event ProgressReporter Progress;
}
Code within the Broadcaster type has full access to Progress and can treat it as a
delegate. Code outside of Broadcaster can only perform += and ?= operations on the
Progress event.

A lambda expression is an unnamed method written in place of a delegate instance.
The compiler immediately converts the lambda expression to either:
? A delegate instance.
? An expression tree, of type Expression<TDelegate>, representing the code inside
the lambda expression in a traversable object model. This allows the lambda
expression to be interpreted later at runtime
A lambda expression has the following form:
(parameters) => expression-or-statement-block
x => x * x;
x => { return x * x; };
Here’s an example of an expression that accepts two parameters:
Func<string,string,int> totalLength = (s1, s2) => s1.Length + s2.Length;
int total = totalLength ("hello", "world"); // total is 10;

Captured variables are evaluated when the delegate is actually invoked, not when
the variables were captured:
int factor = 2;
Func<int, int> multiplier = n => n * factor;
factor = 10;
Console.WriteLine (multiplier (3)); // 30
Lambda expressions can themselves update captured variables:
int seed = 0;
Func<int> natural = () => seed++;
Console.WriteLine (natural()); // 0
Console.WriteLine (natural()); // 1
Console.WriteLine (seed); // 2

given this delegate:
delegate int Transformer (int i);
we could write and call an anonymous method as follows:
Transformer sqr = delegate (int x) {return x * x;};
Console.WriteLine (sqr(3)); // 9
The first line is semantically equivalent to the following lambda expression:
Transformer sqr = (int x) => {return x * x;};
Or simply:
Transformer sqr = x => x * x;

A unique feature of anonymous methods is that you can omit the parameter declaration
entirely—even if the delegate expects them. This can be useful in declaring
events with a default empty handler:
public event EventHandler Clicked = delegate { };
This avoids the need for a null check before firing the event. The following is also
legal:
Clicked += delegate { Console.WriteLine ("clicked"); }; // No parameters
Anonymous methods capture outer variables in the same way lambda expressions
do.

When an exception is thrown, the CLR performs a test:
Is execution currently within a try statement that can catch the exception?
? If so, execution is passed to the compatible catch block. If the catch block successfully
finishes executing, execution moves to the next statement after the
try statement (if present, executing the finally block first).
? If not, execution jumps back to the caller of the function, and the test is repeated
(after executing any finally blocks that wrap the statement).
If no function takes responsibility for the exception, an error dialog is displayed to
the user, and the program terminates.

A finally block executes either:
? After a catch block finishes
? After control leaves the try block because of a jump statement (e.g., return or
goto)
? After the try block ends

All exceptions in C# are runtime exceptions—there is no equivalent
to Java’s compile-time checked exceptions.

To represent null in a value type, you must use a special construct called a nullable
type. A nullable type is denoted with a value type followed by the ? symbol:
int? i = null; // OK, Nullable Type
Console.WriteLine (i == null); // True

int? y = null;
Console.WriteLine (y != 5); // True
Console.WriteLine (y < 6); // False
Console.WriteLine (y > 6); // False
Lifted equality operators handle nulls just like reference types do. This means two
null values are equal:
Console.WriteLine ( null == null); // True
Console.WriteLine ((bool?)null == (bool?)null); // True
Further:
? If exactly one operand is null, the operands are unequal.
? If both operands are non-null, their Values are compared.
null | true is true and null & false is false

int? a = null, b = 1, c = 2;
Console.WriteLine (a ?? b ?? c); // 1 (first non-null value)

The type
of the first parameter will be the type that is extended. For example:
public static class StringHelper
{
public static bool IsCapitalized (this string s)
{
if (string.IsNullOrEmpty(s)) return false;
return char.IsUpper (s[0]);
}
}
The IsCapitalized extension method can be called as though it were an instance
method on a string, as follows:
Console.WriteLine ("Perth".IsCapitalized());
Any compatible instance method will always take precedence over an extension
method.

An anonymous type is a simple class created by the compiler on the fly to store a set
of values.
var dude = new { Name = "Bob", Age = 1 };

Dynamic binding defers binding—the process of resolving types, members, and
operations—from compile time to runtime. Dynamic binding is useful when at
compile time you know that a certain function, member, or operation exists, but the
compiler does not.
A dynamic type is declared with the contextual keyword dynamic:
dynamic d = GetSomeObject();
d.Quack();

Custom binding occurs when a dynamic object implements IDynamicMetaObjectPro
vider (IDMOP).
using System;
using System.Dynamic;
public class Test
{
static void Main()
{
dynamic d = new Duck();
d.Quack(); // Quack method was called
d.Waddle(); // Waddle method was called
}
}
public class Duck : DynamicObject
{
public override bool TryInvokeMember (
InvokeMemberBinder binder, object[] args, out object result)
{
Console.WriteLine (binder.Name + " method was called");
result = null;
return true;
}
}

Language binding occurs when a dynamic object does not implement
IDynamicMetaObjectProvider.
The var and dynamic types bear a superficial resemblance, but the difference is deep:
? var says, “Let the compiler figure out the type.”
? dynamic says, “Let the runtime figure out the type.”
dynamic x = "hello"; // Static type is dynamic, runtime type is string
var y = "hello"; // Static type is string, runtime type is string
int i = x; // Runtime error
int j = y; // Compile-time error

Implicitly, the target of an attribute is the code element it immediately precedes,
which is typically a type or type member. However, you can also attach attributes
to an assembly. This requires that you explicitly specify the attribute’s target.
Here is an example of using the CLSCompliant attribute to specify CLS compliance
for an entire assembly:
[assembly:CLSCompliant(true)]

C# supports direct memory manipulation via pointers within blocks of code marked
unsafe and compiled with the /unsafe compiler option. Pointer types are primarily
useful for interoperability with C APIs, but may also be used for accessing memory
outside the managed heap or for performance-critical hotspots.
class Test
{
int x;
static void Main()
{
Test test = new Test();
unsafe
{
fixed (int* p = &test.x) // Pins test
{
*p = 9;
}
System.Console.WriteLine (test.x);
}
}
}

In addition to the & and * operators, C# also provides the C++ style -> operator,
which can be used on structs:
struct Test
{
int x;
unsafe static void Main()
{
Test test = new Test();
Test* p = &test;
p->x = 9;
System.Console.WriteLine (test.x);
}
}
Pointers are also useful for accessing data outside the managed heap (such as when
interacting with C DLLs or COM), or when dealing with data not in the main
memory (such as graphics memory or a storage medium on an embedded device).

Preprocessor directives supply the compiler with additional information about regions
of code. The most common preprocessor directives are the conditional directives,
which provide a way to include or exclude regions of code from compilation.
For example:
#define DEBUG
class MyClass
{
int x;
void Foo()
{
# if DEBUG
Console.WriteLine ("Testing: x = {0}", x);
# endif
}
...
}
Preprocessor directives can be passed to the compiler with the /define:symbol commandline
option.
#if TESTMODE && !DEBUG
#warning text text of the warning to appear in compiler output
#error text text of the error to appear in compiler output
#line [ number ["file"] | hidden] number specifies the line in source code; file is the filename to
appear in computer output; hidden instructs debuggers to skip over
code from this point until the next #line directive
#region name Marks the beginning of an outline
#end region Ends an outline region

If you compile with the /doc directive, the compiler extracts and collates documentation
comments into a single XML file.
Third-party tools (such as Sandcastle and NDoc) can transform XML file into
an HTML help file.

These always apply English
culture rules:
Console.WriteLine (char.ToUpperInvariant ('i')); // I
This is a shortcut for:
Console.WriteLine (char.ToUpper ('i', CultureInfo.InvariantCulture))

IndexOf is overloaded to accept a startPosition (an index from which to begin
searching) and a StringComparison enum. The latter allows you to perform caseinsensitive
searches.

IndexOfAny returns the first matching position of any one of a set of characters.

By default, Split uses whitespace characters as delimiters; it’s also overloaded to
accept a params array of char or string delimiters. Split also optionally accepts a
StringSplitOptions enum, which has an option to remove empty entries: this is
useful when words are separated by several delimiters in a row.

The static Join method does the reverse of Split. It requires a delimiter and string
array.
string composite = "It's {0} degrees in {1} on this {2} morning";
string s = string.Format (composite, 35, "Perth", DateTime.Now.DayOfWeek);
If the value is negative, the data
is left-aligned; otherwise, it’s right-aligned. For example:
string composite = "Name={0,-20} Credit Limit={1,15:C}";
Console.WriteLine (string.Format (composite, "Mary", 500));
Console.WriteLine (string.Format (composite, "Elizabeth", 20000));

For string equality comparison, you can use the == operator or one of string’s
Equals methods. The latter are more versatile because they allow you to specify
options such as case-insensitivity.
Despite ordinal’s limitations, string’s == operator always performs ordinal casesensitive
comparison. The same goes for the instance version of string.Equals when
called without arguments; this defines the “default” equality comparison behavior
for the string type.

In the second category are UTF-8, UTF-16, and UTF-32 (and the obsolete UTF-7).
Each differs in space efficiency. UTF-8 is the most space-efficient for most kinds of
text: it uses between one and four bytes to represent each character. The first 128
characters require only a single byte, making it compatible with ASCII. UTF-8 is the
most popular encoding for text files and streams (particularly on the Internet), and
it is the default for stream I/O in .NET (in fact, it’s the default for almost everything
that implicitly uses an encoding).
UTF-16 uses one or two 16-bit words to represent each character, and is what .NET
uses internally to represent characters and strings. Some programs also write files in
UTF-16.

Whether to encode/decode UTF-16/UTF-32 with the most significant bytes
first (big endian) or the least significant bytes first (little endian). The default is
little endian, the standard on the Windows operating system.
.NET stores characters and strings in UTF-16. Because UTF-16
requires one or two 16-bit words per character, and a char is only 16 bits in length,
some Unicode characters require two chars to represent. This has a couple of
consequences:
? A string’s Length property may be greater than its real character count.
? A single char is not always enough to fully represent a Unicode character.

Console.WriteLine (new TimeSpan (2, 30, 0)); // 02:30:00
Console.WriteLine (TimeSpan.FromHours (2.5)); // 02:30:00
Console.WriteLine (TimeSpan.FromHours (?2.5)); // ?02:30:00
TimeSpan overloads the < and > operators, as well as the + and ? operators. The
following expression evaluates to a TimeSpan of 2.5 hours:
TimeSpan.FromHours(2) + TimeSpan.FromMinutes(30);
The next expression evaluates to one second short of 10 days:
TimeSpan.FromDays(10) - TimeSpan.FromSeconds(1); // 9.23:59:59
In contrast, the Total... properties return values of type double describing the entire
time span.
The static Parse method does the opposite of ToString, converting a string to a
TimeSpan.
DateTime and DateTimeOffset are immutable structs for representing a date, and optionally,
a time. They have a resolution of 100 ns, and a range covering the years
0001 through 9999.
DateTimeOffset was added in Framework 3.5 and is functionally similar to
DateTime. Its distinguishing feature is that it also stores a UTC offset; this allows
more meaningful results when comparing values across different time zones.
DateTime also provides a Today property that returns just the date portion:
Console.WriteLine (DateTime.Today); // 11/11/2007 12:00:00 AM
Calling ToString on a DateTime formats the result as a short date (all numbers) followed
by a long time (including seconds). For example:
13/02/2000 11:50:30 AM
The operating system’s control panel, by default, determines such things as whether
the day, month, or year
The ToShortDateString and ToLongDateString methods return just the date portion.
The long date format is also determined by the control panel; an example is “Saturday,
17 February 2007”. ToShortTimeString and ToLongTimeString return just the
time portion, such as 17:10:10 (the former excludes seconds).

parsing “1.234” into a
double gives us 1234 in Germany:
Console.WriteLine (double.Parse ("1.234")); // 1234 (In Germany)
This is because in Germany, the period indicates a thousands separator rather than
a decimal point. Specifying invariant culture fixes this:
double x = double.Parse ("1.234", CultureInfo.InvariantCulture);
The same applies when calling ToString():
string x = 1.234.ToString (CultureInfo.InvariantCulture);

The gateway to using a format provider is IFormattable. All numeric types—and
DateTime(Offset)—implement this interface:
public interface IFormattable
{
string ToString (string format, IFormatProvider formatProvider);
}
The .NET Framework defines three format providers (all of which implement
IFormatProvider):
NumberFormatInfo
DateTimeFormatInfo
CultureInfo
Numeric Format Strings Table 6-3 lists custom numeric format strings.
Table 6-4. Culture-sensitive date/time format strings

Sometimes you need to include binary data such as a bitmap within a text document
such as an XML file or email message. Base 64 is a ubiquitous means of encoding
binary data as readable characters, using 64 characters from the ASCII set.
If you’re dealing with data that’s originated from or destined for an XML file,
XmlConvert (the System.Xml namespace) provides the most suitable methods for formatting
and parsing. The methods in XmlConvert handle the nuances of XML
formatting without needing special format strings.
TypeConverter for the Color type (in the System.Drawing namespace, System.Drawing.
dll):
TypeConverter cc = TypeDescriptor.GetConverter (typeof (Color));
Color beige = (Color) cc.ConvertFromString ("Beige");

Using the same seed guarantees the same series of numbers,
which is sometimes useful when you want reproducibility:
Random r1 = new Random (1);
Random r2 = new Random (1);
Console.WriteLine (r1.Next (100) + ", " + r1.Next (100)); // 24, 11
Console.WriteLine (r2.Next (100) + ", " + r2.Next (100)); // 24, 11
If you don’t want reproducibility, you can construct Random with no seed—then it
uses the current system time to make one up.
Calling Next(n) generates a random integer between 0 and n?1. NextDouble generates
a random double between 0 and 1. NextBytes fills a byte array with random values.

double x = double.NaN;
Console.WriteLine (x == x); // False
Console.WriteLine (x.Equals (x)); // True
var sb1 = new StringBuilder ("foo");
var sb2 = new StringBuilder ("foo");
Console.WriteLine (sb1 == sb2); // False (referential equality)
Console.WriteLine (sb1.Equals (sb2)); // True (value equality)
A struct’s Equals method applies structural value equality by default (i.e., it
compares each field in the struct).

If you don’t redirect output, Process.Start executes the program in parallel to the
caller. If you want to wait for the new process to complete, you can call WaitFor
Exit on the Process object, with an optional timeout.

To create a deep copy—where reference type subobjects are duplicated—you must
loop through the array and clone each element manually. The same rules apply to
other .NET collection types.

The binary search methods are fast, but they work only on sorted arrays and require
that the elements be compared for order, rather than simply equality.

The predicate-based searching methods allow a method delegate or lambda expression
to arbitrate on whether a given element is a “match.” A predicate is simply a
delegate accepting an object and returning true or false:
public delegate bool Predicate<T> (T object);
In the following example, we search an array of strings for a name containing the
letter “a”:
static void Main()
{
string[] names = { "Rodney", "Jack", "Jill" };
string match = Array.Find (names, ContainsA);
Console.WriteLine (match); // Jack
}
static bool ContainsA (string name) { return name.Contains ("a"); }
Here’s the same code shortened with an anonymous method:
string[] names = { "Rodney", "Jack", "Jill" };
string match = Array.Find (names, delegate (string name)
{ return name.Contains ("a"); } );
A lambda expression shortens it further:
string[] names = { "Rodney", "Jack", "Jill" };
string match = Array.Find (names, n => n.Contains ("a")); // Jack

Array provides shallow copying and cloning methods.
AsReadOnly returns a wrapper that prevents elements from being reassigned.
The Resize method works by creating a new array and copying over the elements,
returning the new array via the reference parameter. However, any references to the
original array in other objects will remain unchanged.

The generic List and nongeneric ArrayList classes provide a dynamically sized array
of objects and are among the most commonly used of the collection classes. Array
List implements IList, whereas List<T> implements both IList and IList<T>.

LINQ, or Language Integrated Query, is a set of language and framework features
for writing structured type-safe queries over local object collections and remote data
sources. LINQ was introduced in C# 3.0 and Framework 3.5.

IEnumerable<string> filteredNames = names.Where (n => n.Contains ("a"));
foreach (string name in filteredNames)
Console.WriteLine (name); // Harry
C# also provides another
syntax for writing queries, called query expression syntax. Here’s our preceding
query written as a query expression:
IEnumerable<string> filteredNames = from n in names
where n.Contains ("a")
select n;

string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
IEnumerable<string> query = names
.Where (n => n.Contains ("a"))
.OrderBy (n => n.Length)
.Select (n => n.ToUpper());
foreach (string name in query) Console.WriteLine (name);

#define TESTMODE // #define directives must be at top of file
csc Program.cs /define:TESTMODE,PLAYMODE
[Conditional ("LOGGINGMODE")]
static void LogStatus (string msg)
{
...
}
The Conditional attribute is ignored at runtime—it’s purely an instruction to the
compiler.
public static bool AddIfNotPresent<T> (IList<T> list, T item)
{
Contract.Requires (list != null); // Precondition
Contract.Requires (!list.IsReadOnly); // Precondition
...
}
Contract.Assert (x == 3); // Fail unless x is 3

Stopwatch s = Stopwatch.StartNew();
System.IO.File.WriteAllText ("test.txt", new string ('*', 30000000));
Console.WriteLine (s.Elapsed); // 00:00:01.4322661

As a rule, streams are not thread-safe, meaning that two threads cannot concurrently
read or write to the same stream without possible error. The Stream class offers a
simple workaround via the static Synchronized method. This method accepts a
stream of any type and returns a thread-safe wrapper.

The FileSystemWatcher class lets you monitor a directory (and optionally, subdirectories)
for activity.
Because FileSystemWatcher raises events on a separate thread,
you must exception-handle the event handling code to prevent
an error from taking down the application.

Although an ordinary FileStream allows random file I/O (by setting the stream’s
Position property), it’s optimized for sequential I/O. As a rough rule of thumb:
? FileStreams are 10 times faster than memory-mapped files for sequential I/O.
? Memory-mapped files are 10 times faster than FileStreams for random I/O.
A FileStream is also unsuitable for multithreaded access—
because its position changes as it is read or written.

Suppose you’ve written an application comprising 10 assemblies: 1 main executable
file, plus 9 DLLs. Although such granularity can be great for design and debugging,
it’s also good to be able to pack the whole thing into a single “click and run”
executable—without demanding the user perform some setup or file extraction ritual.
You can accomplish this by including the compiled assembly DLLs in the main
executable project as embedded resources, and then writing an AssemblyResolve
event handler to load their binary images on demand.

In particular, threads share (heap) memory with other
threads running in the same application. This, in part, is why threading is useful:
one thread can fetch data in the background, for instance, while another thread can
display the data as it arrives.
While a thread waits during a Sleep or Join, it’s said to be
blocked.
The static Thread.CurrentThread property gives you the currently executing thread

for (int i = 0; i < 10; i++)
{
int temp = i;
new Thread (() => Console.Write (temp)).Start();
}

Each thread gets a separate copy of the cycles variable as it enters the Go method
and so is unable to interfere with another concurrent thread. The CLR and operating
system achieve this by assigning each thread its own private memory stack for local
variables.

Meanwhile,
the main thread exits, but the application keeps running because a foreground thread
is still alive.
When a process terminates in this manner, any finally blocks in the execution stack
of background threads are circumvented. This is a problem if your program employs
finally (or using) blocks to perform cleanup work such as releasing resources or
deleting temporary files.

If a user uses the Task Manager to forcibly end a .NET
process, all threads “drop dead” as though they were background
threads. This is observed rather than documented behavior,
and it could vary depending on the CLR and operating
system version.

Whenever you start a thread, a few hundred microseconds are spent organizing such
things as a fresh private local variable stack. Each thread also consumes (by default)
around 1 MB of memory. The thread pool cuts these overheads by sharing and recycling
threads, allowing multithreading to be applied at a very granular level without
a performance penalty.

Any unhandled exceptions are conveniently rethrown onto the
host thread when you call a task’s Wait method. (If you don’t
call Wait and abandon the task, an unhandled exception will
shut down the process, as with an ordinary thread.)

A thread is deemed blocked when its execution is paused for some reason, such as
when Sleeping or waiting for another to end via Join or EndInvoke. A blocked thread
immediately yields its processor time slice, and from then on consumes no processor
time until its blocking condition is satisfied. You can test for a thread being blocked
via its ThreadState property:
bool blocked = (someThread.ThreadState & ThreadState.WaitSleepJoin) != 0;

As a basic rule, you need to lock around accessing any writable shared field.
Even in
the simplest case—an assignment operation on a single field—you must consider
synchronization.

The popular advice, “lock objects in a consistent order to avoid deadlocks,” although
helpful in our initial example, is hard to apply to the scenario just described. A better
strategy is to be wary of locking around calling methods in objects that may have
references back to your own object. Also, consider whether you really need to lock
around calling methods in other classes (often you do—as we’ll see in “Thread
Safety” on page 543—but sometimes there are other options). Relying more on declarative
and data parallelism (Chapter 22), immutable types, and nonblocking synchronization
constructs (later in this chapter) can lessen the need for locking.

A Mutex is like a C# lock, but it can work across multiple processes. In other words,
Mutex can be computer-wide as well as application-wide.
Acquiring and releasing an uncontended Mutex takes a few microseconds—
about 50 times slower than a lock.

A common use for a cross-process Mutex is to ensure that only one instance of a
program can run at a time. Here’s how it’s done.

This is a common pattern throughout the .NET Framework:
static members are thread-safe; instance members are not.

The MSDN documentation states that use of the volatile keyword
ensures that the most up-to-date value is present in the
field at all times. This is incorrect, since as we’ve seen, a write
followed by a read can be reordered.
The volatile keyword is not supported with pass-by-reference arguments or
captured local variables: in these cases, you must use the VolatileRead and
VolatileWrite methods.

The .NET Framework provides four timers. Two of these are general-purpose
multithreaded timers:
? System.Threading.Timer
? System.Timers.Timer
The other two are special-purpose single-threaded timers:
? System.Windows.Forms.Timer (Windows Forms timer)
? System.Windows.Threading.DispatcherTimer (WPF timer)

The CLR includes a marshaler that knows how to convert parameters and return
values between .NET types and unmanaged types.
IntPtr is a struct designed to encapsulate an unmanaged handle, and is 32 bits wide
on 32-bit platforms and 64 bits wide on 64-bit platforms.
With the MarshalAs attribute, you can tell the CLR marshaler the variation in use,
so it can provide the correct translation. Here’s an example:
[DllImport("...")]
static extern int Foo ( [MarshalAs (UnmanagedType.LPStr)] string s );
The UnmanagedType enumeration includes all the Win32 and COM types that the
marshaler understands. In this case, the marshaler was told to translate to LPStr,
which is a null-terminated single-byte ANSI string.

Receiving strings from unmanaged code back to .NET requires that some memory
management take place. The marshaler performs this work automatically if you
declare the external method with a StringBuilder rather than a string.

If you are unsure how to call a particular Win32 method, you
will usually find an example on the Internet if you search for the
method name and DllImport. The site http://www.pinvoke.net
is a wiki that aims to document all Win32 signatures.
Sometimes you need to pass a struct to an unmanaged method.
In both C and C#, fields in an object are located at n number of bytes from the
address of that object. The difference is that in a C# program, the CLR finds this
offset by looking it up using the field name; C field names are compiled directly into
offsets. For instance, in C, wDay is just a token to represent whatever is at the address
of a SystemTime instance plus 24 bytes.

The P/Invoke layer does its best to present a natural programming model on both
sides of the boundary, mapping between relevant constructs where possible. Since
C# can not only call out to C functions but also can be called back from the C
functions (via function pointers), the P/Invoke layer needs to map unmanaged function
pointers onto something natural for the managed world. The managed equivalent
of a function pointer is a delegate, so the P/Invoke layer automatically maps
between delegates (in C#) and function pointers (in C).

Memory-mapped files, or shared memory, is a feature in Windows that allows
multiple processes on the same computer to share data, without the overhead of
Remoting or WCF. Shared memory is extremely fast and, unlike pipes, offers
random access to the shared data.

Marshal.AllocHGlobal allocates memory on the unmanaged heap. Here’s how to
later free the same memory:
Marshal.FreeHGlobal (new IntPtr (data));