Conversion operators
The cast operator is not overloadable but you can write a conversion operator method which lives in the target class. Conversion methods can define two varieties of operators, implicit and explicit conversion operators. The implicit operator will cast without specifying with the cast operator (( )) and the explicit operator requires it to be used.
Implicit conversion operator
class Foo
{
public int Value; public static implicit operator Foo(int value) { return new Foo(value) }}
//Implicit conversion
Foo foo = 2; Explicit conversion operator
class Foo
{
public int Value; public static explicit operator Foo(int value) { return new Foo(value) }}
//Explicit conversion
Foo foo = (Foo)2; as operator
The as operator will attempt to do a silent cast to a given type. If it succeeds it will return the object as the new type, if it fails it will return a null reference.
Stream stream = File.Open(@"C:\Temp\data.dat");FileStream fstream = stream as FileStream; //Will return an object. String str = stream as String; //Will fail and return null.
Null-Coalesce operator
The following
return maybeNull ?? def;
Is shorthand for
return maybeNull != null ? maybeNull : def;
One can also have nullable scalar types such as
int? i = null;

