Wednesday, July 29, 2015

emacs users ergonomic concern

Emacs users are heavy function/modifier key users. I have followed originally the general idea to swap left ctrl with caplock, but the pinky finger really gets sore after days. Now by article from Xah Lee (, I realize that I need a ergo keyboard as well as swap ctrl with alt.

It is annoying to swtich function key at the time that I am already used to the original positions, but for healthy concideration, it ought to be good for long run!

I am now looking for good keyboards and try to get used to ctrl-alt swapped layout.

Happy emacsing!

Saturday, July 25, 2015

Officially, I am entitled as an Emacs beginner :)

I have studied emacs for couple of weeks and spent numerous time tinkering it. So much fun to learn such amazing text editing tool :D.

until today, I found out that people should rebind cap-lock key to ctrl to ease the left hand muscles! I have used by-default ctrl key for these days and didn't find it difficult to use after several try days.

now I have to reshape the ctrl key memory in head, but surely it will take little time!

Saturday, July 18, 2015

C# IDisposable best practice

General Information

  • .net managed code talks to and uses external resources, that needs to implement IDisposable interface
  • GC(Garbage Collector) tries best to clean memory when using external resources, such as SQLconnection, without IDisposable. Thus, it is impossible to detect when app will fail.
  • GC is triggered when the memory threshold is reached.

Managed resources v.s. unmanaged resources

Managed resources basically means "managed memory" that is managed by the garbage collector. When you no longer have any references to a managed object (which uses managed memory), the garbage collector will (eventually) release that memory for you.

Unmanaged resources are then everything that the garbage collector does not know about. For example:
Open files
Open network connections
Unmanaged memory
In XNA: vertex buffers, index buffers, textures, etc.

Normally you want to release those unmanaged resources before you lose all the references you have to the object managing them. You do this by calling Dispose on that object, or (in C#) using the using statement which will handle calling Dispose for you.

If you neglect to Dispose of your unmanaged resources correctly, the garbage collector will eventually handle it for you when the object containing that resource is garbage collected (this is "finalization"). But because the garbage collector doesn't know about the unmanaged resources, it can't tell how badly it needs to release them - so it's possible for your program to perform poorly or run out of resources entirely.

If you implement a class yourself that handles unmanaged resources, it is up to you to implement Dispose and Finalize correctly.

Best Practices

  • best practice #1: Dispose of IDisposable objects as soon as you can. 
  • Implementing IDisposable code pattern as:
  •  private bool _disposed;   
      public void Dispose()    
       // Use SupressFinalize in case a subclass    
       // of this type implements a finalizer.    
      protected virtual void Dispose(bool disposing)    
       if (!_disposed)    
       if (disposing)    
        // Clear all property values that maybe have been set    
        // when the class was instantiated    
        id = 0;    
        name = String.Empty;    
        pass = String.Empty;    
       // Indicate that the instance has been disposed.    
       _disposed = true;    
  • best practice #2: If you use IDisposable objects (e.g. SQLConnection _connection) as instance fields, implement IDisposable.
  • best practice #3: allow Dispose() to be called multiple times and do not throw exceptions (last 3 lines in the code sample below)
  •  public class DatabaseState : IDisposable   
       private SqlConnection _connection;   
      public void Dispose()   
      protected virtual void Dispose(bool disposing)   
       if(disposing && _connection != null)   
        { _connection.Dispose();   
        _connection = null;   
  • best practice #4 implement IDisposable to support disposing resources in a class heraychy.
  • best practice #5 if you use unmanaged resources, declare a finalizer which cleans them up.
  • best practice #6: Visual Studio using code analysis CA2000 (not enabled by default) to check this issue.
  • best practice #7: if your class implements an interface and use IDisposable fields, extend the interface from IDisposable rather than the implementing class. This conflicts with OO Design.
  • best practice #8: if you implement IDisposable, do not implement it explicitly.

Friday, July 17, 2015

C# Generic study

Generic Constraints

Generic Constraints is used when T in the class needs to have specific characteristics/methods that object type doesn't have (object type has only "toString" and "hasHashCode" methods).
  • "where" keyword can be used to constrain the generic type, for example: 
    • public class SqlRepository<T> : IRepository<T> where T : class
  • we can create a new interface with such method and use "where" keyword to constrain T. for example:
    • public interface IEntity {bool IsValid()} so that "where T : IEntity" will allow "T.IsValid()".
    • Note. usually we do not need to define own interface, check in advance if .net framework already has such!
  • default(T) will assign default value to T. if T is class -> null; if T is struct -> 0
  • where T : new() -> T has a default constructor so that T can be instantiated in code, for example:
    • public T CreateNewT() {T t = new T(); return t;}
  • constraints is preferable to be implemented in concrete class instead of in interface
  • Covariant V.S. Contravariant
  •  public interface IReadOnlyRepository<out T> : IDisposable  
         T FindById(int id);  
         IQueryable<T> FindAll();  
       public interface IWriteOnlyRepository<in T> : IDisposable  
         void Add(T newEntity);  
         void Delete(T entity);  
         int Commit();  
       public interface IRepository<T> : IReadOnlyRepository<T>, IWriteOnlyRepository<T>  
     using (IRepository<Employee> employeeRepository   
             = new SqlRepository<Employee>(new EmployeeDb()))  
  • Reflection in C# (for Generic part)  // ToDo

Thursday, July 16, 2015

Study HashSet and SortedList generics and remove generics in code of business logic

Note. This lesson is learned from Scott Allen C# Generic videos in Pluralsight.

1) Hashset (how to remove object duplicates)

HashSet type is supposed to identify duplicates and guarantee the uniqueness of stored objects, while the uniqueness/equality comparison shall be defined by the code.

HashSet has one construction that accepts a IEqualityComparer interface.

var departments = new SortedDictionary<string, HashSet<Employee>>();
Departments.Add("Sales", new HashSet<Employee>(new EmployeeComparer());
departments["Sales"].Add(new Employee{Name = "Joy" });
departments["Sales"].Add(new Employee{Name = "Joy" });
departments["Sales"].Add(new Employee{Name = "Joy" });

2) SortedSet (how to sort objects)

//SortedSet is similar, needs to implement IComparer<> interface
new SortedSet<Employee>(new EmployeeComparer());

public class EmployeeComparer : IEqualityComparer<Employee>, IComparer<Employee>
Equals ();
GetHashCode ();

3) Cleaner code - hide those fussy "new", "generic" keywords by create a new class to inherit

namespace Employees  
   public class Employee  
     public string Name { get; set; }  
   public class EmployeeComparer : IEqualityComparer<Employee>, IComparer<Employee>  
     public bool Equals(Employee x, Employee y)  
       return string.Equals(x.Name, y.Name);  
     public int GetHashCode(Employee obj)  
       return obj.Name.GetHashCode();  
     public int Compare(Employee x, Employee y)  
       return string.Compare(x.Name, y.Name);  
   public class DepartmentCollection : SortedDictionary<string, HashSet<Employee>>  
     public DepartmentCollection Add(string departmentName, Employee employee)  
       if (!ContainsKey(departmentName)) {   
         this.Add(departmentName, new HashSet<Employee>(new EmployeeComparer()));  
       return this;  
   class Program  
     static void Main(string[] args)  
       var departments = new DepartmentCollection();  
       departments.Add("Sales", new Employee { Name = "Xi" })  
             .Add("Sales", new Employee { Name = "Xi" })  
             .Add("Sales", new Employee { Name = "Liu" });  
       departments.Add("Engineers", new Employee { Name = "Jin" })  
             .Add("Engineers", new Employee { Name = "Jin" })  
             .Add("Engineers", new Employee { Name = "Jin" });  
       foreach (var item in departments)  
         foreach (var employee in item.Value)  
           Console.WriteLine("\t" + employee.Name);  

Monday, July 13, 2015

[book read] Win Friends influence people

This is a mind blowing book for me!

In order to get the most out of this book:

  • Develop a deep, driving desire to master the principles of human relations
  • Read each chapter twice before going on to the next one
  • Stop frequently to ask self how you can apply each suggestion
  • Underscore each important idea
  • Review this book each month :)
  • Apply these principles at every opportunity!
  • make a lovely game out of the learning
  • Check up each week on the progress you are making. Ask yourself what mistakes you have made, what improvement, what lessons you have learned for the future
  • Keep notes

Summary of the book

  1. Don't criticize, condemn or complain
  2. Give honest and sincere appreciation
  3. Arouse in the other person an eager want

Six ways to make people like you
  1. Become genuinely interested in other people
  2. Smile
  3. Remember that a person's name is to that person the sweetest and most important sound in any language
  4. Be a good listener. Encourage others to talk about themselves
  5. Talk in terms of the other person's interests
  6. Make the other person feel important and do it sincerely

Sunday, July 5, 2015

C# in a Nutshell Chapter 15 - Streams and I/O

The .NET stream architecture centers on three concepts: backing stores, decorators, and adapters.

Backing store streams
These are hard-wired to a particular type of backing store, such as FileStream or NetworkStream

Decorator streams
These feed off another stream, transforming the data in some way, such as DeflateStream or CryptoStream

Both backing store and decorator streams deal exclusively in bytes. Although this is flexible and efficient, applications often work at higher levels such as text or XML. Adapters bridge this gap by wrapping a stream in a class with specialized methods typed to a particular format. For example, a text reader exposes a ReadLine method; an XML writer exposes a WriteAttributes method.

An adapter wraps a stream, just like a decorator. Unlike a decorator, however, an adapter is not itself a stream; it typically hides the byte-oriented methods completely.

To summarize, backing store streams provide the raw data; decorator streams provide transparent binary transformations such as encryption; adapters offer typed methods for dealing in higher-level types such as strings and XML. To compose a chain, you simply pass one object into another’s constructor.

Stream Class

using System;
using System.IO;
class Program
static void Main()
// Create a file called test.txt in the current directory:
using (Stream s = new FileStream ("test.txt", FileMode.Create))
Console.WriteLine (s.CanRead);
// True
Console.WriteLine (s.CanWrite);
// True
Console.WriteLine (s.CanSeek);
// True

s.WriteByte (101);
s.WriteByte (102);
byte[] block = { 1, 2, 3, 4, 5 };
s.Write (block, 0, block.Length); // Write block of 5 bytes
Console.WriteLine (s.Length);
Console.WriteLine (s.Position);
s.Position = 0; // 7
// 7
// Move back to the start
Console.WriteLine (s.ReadByte());
Console.WriteLine (s.ReadByte()); // 101
// 102
// Read from the stream back into the block array:
Console.WriteLine (s.Read (block, 0, block.Length)); // 5
// Assuming the last Read returned 5, we'll be at
// the end of the file, so Read will now return 0:
Console.WriteLine (s.Read (block, 0, block.Length)); // 0

Reading and Writing

A stream may support reading, writing, or both. If CanWrite returns false , the stream is read-only; if CanRead returns false , the stream is write-only.

With Read , you can be certain you’ve reached the end of the stream only when the method returns 0 . So, if you have a 1,000 byte stream, the following code may fail to read it all into memory:

// Assuming s is a stream:
byte[] data = new byte [1000];
s.Read (data, 0, data.Length);

The Read method could read anywhere from 1 to 1,000 bytes, leaving the balance of the stream unread.

Here’s the correct way to read a 1,000-byte stream:

byte[] data = new byte [1000];
// bytesRead will always end up at 1000, unless the stream is itself smaller in length:
int bytesRead = 0;
int chunkSize = 1;
while (bytesRead < data.Length && chunkSize > 0)
bytesRead += chunkSize = s.Read (data, bytesRead, data.Length - bytesRead);

Fortunately, the BinaryReader type provides a simpler way to achieve the same result:

byte[] data = new BinaryReader (s).ReadBytes (1000);

If the stream is less than 1,000 bytes long, the byte array returned reflects the actual stream size. If the stream is seekable, you can read its entire contents by replacing 1000 with (int)s.Length .


A stream is seekable if CanSeek returns true . With a seekable stream (such as a file stream), you can query or modify its Length (by calling SetLength ), and at any time change the Position at which you’re reading or writing. The Position property is relative to the beginning of the stream; the Seek method, however, allows you to move relative to the current position or the end of the stream.

With a nonseekable stream (such as an encryption stream), the only way to determine its length is to read it right through. Furthermore, if you need to reread a previous section, you must close the stream and start afresh with a new one.

Closing and Flush

Streams must be disposed after use to release underlying resources such as file and socket handles. A simple way to guarantee this is by instantiating streams within using blocks.

In general, streams follow standard disposal semantics:

  • Dispose and Close are identical in function.
  • Disposing or closing a stream repeatedly causes no error.
Closing a decorator stream closes both the decorator and its backing store stream. With a chain of decorators, closing the outermost decorator (at the head of the chain) closes the whole lot.

Some streams internally buffer data to and from the backing store to lessen round tripping and so improve performance (file streams are a good example of this). This means data you write to a stream may not hit the backing store immediately; it can be delayed as the buffer fills up. The Flush method forces any internally buffered data to be written immediately. Flush is called automatically when a stream is closed, so you never need to do the following: s.Flush(); s.Close();


A stream supports read and write timeouts if CanTimeout returns true . Network streams support timeouts; file and memory streams do not. For streams that support timeouts, the ReadTimeout and WriteTimeout properties determine the desired timeout in milliseconds, where 0 means no timeout. The Read and Write methods indicate that a timeout has occurred by throwing an exception.

Backing Store Streams


The simplest way to instantiate a FileStream is to use one of the following static methods on the File class:

FileStream fs1 = File.OpenRead ("readme.bin");  // Read-only
FileStream fs2 = File.OpenWrite (@"c:\temp\writeme.tmp");  // Write-only
FileStream fs3 = File.Create (@"c:\temp\writeme.tmp"); // Read/write

OpenWrite and Create differ in behavior if the file already exists. Create truncates any existing content; OpenWrite leaves existing content intact with the stream positioned at zero. If you write fewer bytes than were previously in the file, OpenWrite leaves you with a mixture of old and new content.

Instantiate a FileStream is also possible. The following opens an existing file for read/write access without overwriting it:

var fs = new FileStream ("readwrite.tmp", FileMode.Open);

File Class

The following static methods read an entire file into memory in one step:
• File.ReadAllText (returns a string)
• File.ReadAllLines (returns an array of strings)
• File.ReadAllBytes (returns a byte array)
The following static methods write an entire file in one step:
• File.WriteAllText
• File.WriteAllLines
• File.WriteAllBytes
• File.AppendAllText (great for appending to a log file)

There’s also a static method called File.ReadLines : this is like ReadAllLines except that it returns a lazily-evaluated IEnumerable<string> . This is more efficient because it doesn’t load the entire file into memory at once. LINQ is ideal for consuming the results: the following calculates the number of lines greater than 80 characters in length:
int longLines = File.ReadLines ("filePath").Count (l => l.Length > 80);

File Mode


Closing and flushing a MemoryStream is optional. If you close a MemoryStream , you can no longer read or write to it, but you are still permitted to call ToArray to obtain the underlying data.
Flush does absolutely nothing on a memory stream.


PipeStream was introduced in Framework 3.5. It provides a simple means by which one process can communicate with another through the Windows pipes protocol.
There are two kinds of pipe:

  1. Anonymous pipe: Allows one-way communication between a parent and child process on the same computer.
  2. Named pipe: Allows two-way communication between arbitrary processes on the same computer—or different computers across a Windows network.

PipeStream is an abstract class with four concrete subtypes. Two are used for anonymous pipes and the other two for named pipes:

  1. AnonymousPipeServerStream and AnonymousPipeClientStream
  2. NamedPipeServerStream and NamedPipeClientStream


BufferedStream decorates, or wraps, another stream with buffering capability.
Buffering improves performance by reducing round trips to the backing store. Here’s how we wrap a FileStream in a 20 KB BufferedStream :

// Write 100K to a file:
File.WriteAllBytes ("myFile.bin", new byte [100000]);
using (FileStream fs = File.OpenRead ("myFile.bin"))
using (BufferedStream bs = new BufferedStream (fs, 20000))
Console.WriteLine (fs.Position);
// 20000

In this example, the underlying stream advances 20,000 bytes after reading just 1 byte, thanks to the read-ahead buffering. We could call ReadByte another 19,999 times before the FileStream would be hit again.

Coupling a BufferedStream to a FileStream , as in this example, is of limited value because FileStream already has built-in buffering. Its only use might be in enlarging the buffer on an already constructed FileStream .
Closing a BufferedStream automatically closes the underlying backing store stream.

Stream Adapters

A Stream deals only in bytes; to read or write data types such as strings, integers, or XML elements, you must plug in an adapter.

Text Adapters

TextReader and TextWriter are the abstract base classes for adapters that deal exclusively with characters and strings.

using (FileStream fs = File.Create ("test.txt"))
using (TextWriter writer = new StreamWriter (fs))
writer.WriteLine ("Line1");
writer.WriteLine ("Line2");
using (FileStream fs = File.OpenRead ("test.txt"))
using (TextReader reader = new StreamReader (fs))
Console.WriteLine (reader.ReadLine()); // Line1
Console.WriteLine (reader.ReadLine()); // Line2

Because text adapters are so often coupled with files, the File class provides the static methods CreateText , AppendText , and OpenText to shortcut the process:

using (TextWriter writer = File.CreateText ("test.txt"))
writer.WriteLine ("Line1");
writer.WriteLine ("Line2");
using (TextWriter writer = File.AppendText ("test.txt"))
writer.WriteLine ("Line3");
using (TextReader reader = File.OpenText ("test.txt"))
while (reader.Peek() > −1)
Console.WriteLine (reader.ReadLine());

This also illustrates how to test for the end of a file (viz. reader.Peek() ). Another option is to read until reader.ReadLine returns null.

You can also read and write other types such as integers, but because TextWriter invokes ToString on your type, you must parse a string when reading it back:

using (TextWriter w = File.CreateText ("data.txt"))
w.WriteLine (123);
// Writes "123"
w.WriteLine (true);
// Writes the word "true"
using (TextReader r = File.OpenText ("data.txt"))
int myInt = int.Parse (r.ReadLine());
// myInt == 123
bool yes = bool.Parse (r.ReadLine());
// yes == true

Character encodings

TextReader and TextWriter are by themselves just abstract classes with no connection to a stream or backing store. The StreamReader and StreamWriter types, however, are connected to an underlying byte-oriented stream, so they must convert between characters and bytes. They do so through an Encoding class from the System.Text namespace, which you choose when constructing the StreamReader or StreamWriter . If you choose none, the default UTF-8 encoding is used.

StringReader and StringWriter

The StringReader and StringWriter adapters don’t wrap a stream at all; instead, they use a string or StringBuilder as the underlying data source. This means no byte translation is required—in fact, the classes do nothing you couldn’t easily achieve with a string or StringBuilder coupled with an index variable. Their advantage, though, is that they share a base class with StreamReader / StreamWriter . For instance, suppose we have a string containing XML and want to parse it with an XmlReader .
The XmlReader.Create method accepts one of the following:

  1. A URI
  2. A Stream
  3. A TextReader

So, how do we XML-parse our string? Because StringReader is a subclass of TextReader , we’re in luck. We can instantiate and pass in a StringReader as follows:

XmlReader r = XmlReader.Create (new StringReader (myString));

Binary Adapters

BinaryReader and BinaryWriter read and write native data types: bool , byte , char ,
decimal , float , double , short , int , long , sbyte , ushort , uint , and ulong , as well as

string s and arrays of the primitive data types.

BinaryReader can also read into byte arrays. The following reads the entire contents
of a seekable stream:

byte[] data = new BinaryReader (s).ReadBytes ((int) s.Length);

This is more convenient than reading directly from a stream, because it doesn't require a loop to ensure that all data has been read.

Compression Streams

Two general-purpose compression streams are provided in the System.IO.Compression namespace: DeflateStream and GZipStream.

DeflateStream and GZipStream are decorators; they compress or decompress data from another stream that you supply in construction. In the following example, we compress and decompress a series of bytes, using a FileStream as the backing store:

using (Stream s = File.OpenRead ("compressed.bin"))
using (Stream ds = new DeflateStream (s, CompressionMode.Decompress))
for (byte i = 0; i < 100; i++)
Console.WriteLine (ds.ReadByte());
// Writes 0 to 99

Even with the smaller of the two algorithms, the compressed file is 241 bytes long: more than double the original! Compression works poorly with “dense,” nonrepetitive binary filesdata!

In the next example, we compress and decompress a text stream composed of 1,000 words chosen randomly from a small sentence. This also demonstrates chaining a backing store stream, a decorator
stream, and an adapter (as depicted at the start of the chapter in Figure 15-1), and the use asynchronous methods:

string[] words = "The quick brown fox jumps over the lazy dog".Split();
Random rand = new Random();
using (Stream s = File.Create ("compressed.bin"))
using (Stream ds = new DeflateStream (s, CompressionMode.Compress))
using (TextWriter w = new StreamWriter (ds))
for (int i = 0; i < 1000; i++)
await w.WriteAsync (words [rand.Next (words.Length)] + " ");
Console.WriteLine (new FileInfo ("compressed.bin").Length);
// 1073
using (Stream s = File.OpenRead ("compressed.bin"))
using (Stream ds = new DeflateStream (s, CompressionMode.Decompress))
using (TextReader r = new StreamReader (ds))
Console.Write (await r.ReadToEndAsync()); // Output;

In this case, DeflateStream compresses efficiently to 1,073 bytes—slightly more than 1 byte per word.

Compressing in memory

Sometimes you need to compress entirely in memory. Here’s how to use a Memory Stream for this purpose:

byte[] data = new byte[1000];
// We can expect a good compression
// ratio from an empty array!
var ms = new MemoryStream();
using (Stream ds = new DeflateStream (ms, CompressionMode.Compress))
ds.Write (data, 0, data.Length);
byte[] compressed = ms.ToArray();
Console.WriteLine (compressed.Length);
// 113
// Decompress back to the data array:
ms = new MemoryStream (compressed);
using (Stream ds = new DeflateStream (ms, CompressionMode.Decompress))
for (int i = 0; i < 1000; i += ds.Read (data, i, 1000 - i));

The using statement around the DeflateStream closes it in a textbook fashion, flushing any unwritten buffers in the process. This also closes the MemoryStream it wraps —meaning we must then call ToArray to extract its data.

Working with Zip Files

new feature in Framework 4.5 - ZipArchive and ZipFile classes

ZipFile is a static helper class for ZipArchive;

ZipFile ’s CreateFromDirectory method adds all the files in a specified directory into a zip file:
ZipFile.CreateFromDirectory (@"d:\MyFolder", @"d:\");

whereas ExtractToDirectory does the opposite and extracts a zip file to a directory:
ZipFile.ExtractToDirectory (@"d:\", @"d:\MyFolder");

File and Directory Operations

FileInfo offers an easier way to change a file’s read-only flag:
new FileInfo (@"c:\temp\test.txt").IsReadOnly = false;

Here are all the members of the FileAttribute enum that GetAttributes returns:

Archive, Compressed, Device, Directory, Encrypted, Hidden, Normal, NotContentIndexed, Offline, ReadOnly, ReparsePoint, SparseFile, System, Temporary

File security

The GetAccessControl and SetAccessControl methods allow you to query and change the operating system permissions assigned to users and roles via a FileSecurity object (namespace System.Security.AccessControl ). You can also pass a FileSecurity object to a FileStream ’s constructor to specify permissions when creating a new file.

In this example, we list a file’s existing permissions, and then assign execution permission to the “Users” group:

FileSecurity sec = File.GetAccessControl (@"d:\test.txt");
AuthorizationRuleCollection rules = sec.GetAccessRules (true, true,
typeof (NTAccount));
foreach (FileSystemAccessRule rule in rules)
Console.WriteLine (rule.AccessControlType); // Allow or Deny
Console.WriteLine (rule.FileSystemRights); // e.g., FullControl
Console.WriteLine (rule.IdentityReference.Value); // e.g., MyDomain/Joe
var sid = new SecurityIdentifier (WellKnownSidType.BuiltinUsersSid, null);
string usersAccount = sid.Translate (typeof (NTAccount)).ToString();
FileSystemAccessRule newRule = new FileSystemAccessRule
(usersAccount, FileSystemRights.ExecuteFile, AccessControlType.Allow);
sec.AddAccessRule (newRule);
File.SetAccessControl (@"d:\test.txt", sec);

The Directory Class

he static Directory class provides a set of methods analogous to those in the File class—for checking whether a directory exists ( Exists ), moving a directory ( Move ), deleting a directory ( Delete ), getting/setting times of creation or last access, and getting/setting security permissions. Furthermore, Directory exposes the following static methods:

string GetCurrentDirectory ();
SetCurrentDirectory (string path);
DirectoryInfo CreateDirectory (string path);
DirectoryInfo GetParent
(string path);
GetDirectoryRoot (string path);
string[] GetLogicalDrives();
// The following methods all return full paths:
string[] GetFiles
(string path);
string[] GetDirectories
(string path);
string[] GetFileSystemEntries (string path);
IEnumerable<string> EnumerateFiles (string path);
IEnumerable<string> EnumerateDirectories (string path);
IEnumerable<string> EnumerateFileSystemEntries (string path);

FileInfo and DirectoryInfo

The static methods on File and Directory are convenient for executing a single file or directory operation. If you need to call a series of methods in a row, the FileInfo and DirectoryInfo classes provide an object model that makes the job easier.

FileInfo offers most of the File ’s static methods in instance form—with some additional properties such as Extension , Length , IsReadOnly , and Directory —for returning a DirectoryInfo object. For example:

FileInfo fi = new FileInfo (@"c:\temp\FileInfo.txt");
Console.WriteLine (fi.Exists); // false
using (TextWriter w = fi.CreateText())
w.Write ("Some text");
Console.WriteLine (fi.Exists);  // false (still)
Console.WriteLine (fi.Exists);  // true

(fi.Name); // FileInfo.txt
(fi.FullName);  //c:\temp\FileInfo.txt
(fi.DirectoryName); //c:\temp
(fi.Directory.Name); //temp
(fi.Extension); // .txt
(fi.Length); // 9

fi.Attributes ^= FileAttributes.Hidden; //(toggle hidden flag)
fi.IsReadOnly = true;
Console.WriteLine (fi.Attributes); // ReadOnly,Archive,Hidden,Encrypted
Console.WriteLine (fi.CreationTime);

fi.MoveTo (@"c:\temp\FileInfoX.txt");
DirectoryInfo di = fi.Directory;
Console.WriteLine (di.Name);   // temp
Console.WriteLine (di.FullName);  // c:\temp
Console.WriteLine (di.Parent.FullName); // c:\
di.CreateSubdirectory ("SubFolder");

Here’s how to use DirectoryInfo to enumerate files and subdirectories:

DirectoryInfo di = new DirectoryInfo (@"e:\photos");
foreach (FileInfo fi in di.GetFiles ("*.jpg"))
Console.WriteLine (fi.Name);
foreach (DirectoryInfo subDir in di.GetDirectories())
Console.WriteLine (subDir.FullName);

Saturday, July 4, 2015

C# in Nutshell Chapter 13 - Diagnostics and Code Contracts

Conditional Attribute

The Conditional attribute instructs the compiler to ignore any calls to a particular class or method, if the specified symbol has not been defined.

 static void Main()

        public static void WriteLine() { Console.WriteLine("HelloWorld"); }

In Visual Stuido project property -> Build -> Optional compiling symbol to set it

The Conditional attribute is ignored at runtime—it’s purely an instruction to the compiler.

Debug and Trace Classes

Code Contracts

public static bool AddIfNotPresent<T> (IList<T> list, T item)
Contract.Requires (list != null); // Precondition
Contract.Requires (!list.IsReadOnly); // Precondition
Contract.Ensures (list.Contains (item)); // Postcondition
if (list.Contains(item)) return false;
list.Add (item);
return true;

The preconditions are defined by Contract.Requires and are verified when the method starts. The postcondition is defined by Contract.Ensures and is verified not where it appears in the code, but when the method exits. Preconditions and postconditions act like assertions and, in this case, detect the following errors:
  • A bug in the method whereby we forgot to add the item to the list
  • A bug in the method whereby we forgot to add the item to the list
Preconditions and postconditions must appear at the start of the method. 

Windows Eventlog

To write to a Windows event log:
1. Choose one of the three event logs (usually Application).
2. Decide on a source name and create it if necessary.
3. Call EventLog.WriteEntry with the log name, source name, and message data.

The source name is an easily identifiable name for your application. You must register a source name before you use it—the CreateEventSource method performs this function. You can then call WriteEntry :
const string SourceName = "MyCompany.WidgetServer";
// CreateEventSource requires administrative permissions, so this would
// typically be done in application setup.
if (!EventLog.SourceExists (SourceName))
EventLog.CreateEventSource (SourceName, "Application");
EventLog.WriteEntry (SourceName, "Service started; using configuration file=...", EventLogEntryType.Information);

The Stopwatch Class

The Elapsed property returns the elapsed interval as a TimeSpan :

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

C# in Nutshell Chapter 8 - LINQ


A query operator never alters the input sequence; instead, it returns a new sequence. This is consistent with the functional programming paradigm, from which LINQ was inspired.

string[] s = {"Dirk", "Xi", "Auli", "Jouni"};
IEnumerable<string> items = s.where (n => n.length >=4);

Sequence Operators 

Element Operators

Aggregation Operators

Quantifiers (return a bool)

Other Operators

int[] seq1 = { 1, 2, 3 };
int[] seq2 = { 3, 4, 5 };
IEnumerable<int> concat = seq1.Concat (seq2); // { 1, 2, 3, 3, 4, 5 }
IEnumerable<int> union = seq1.Union (seq2); // { 1, 2, 3, 4, 5 }

Query Expression

Query expressions always start with a from clause and end with either a select or
group clause. The from clause declares a range variable (in this case, n), which you
can think of as traversing the input sequence—rather like foreach.

IEnumerable<string> query =
from n in names
where n.Contains ("a") // Filter elements
orderby n.Length // Sort elements
select n.ToUpper(); // Translate each element (project)

The Into Keyword

The into keyword lets you “continue” a query after a projection and is a shortcut
for progressively querying.
The only place you can use into is after a select or group clause. into “restarts” a
query, allowing you to introduce fresh where, orderby, and select clauses.

IEnumerable<string> query =
from n in names
select n.Replace ("a", "").Replace ("e", "").Replace ("i", "")
.Replace ("o", "").Replace ("u", "")
into noVowel
where noVowel.Length > 2 orderby noVowel select noVowel;

Otherwise, we need to write

IEnumerable<string> query =
from n in names
select n.Replace ("a", "").Replace ("e", "").Replace ("i", "")
.Replace ("o", "").Replace ("u", "");
query = from n in query where n.Length > 2 orderby n select n;

Scoping rules
All range variables are out of scope following an into keyword. The following will not compile:
var query =
from n1 in names
select n1.ToUpper()
into n2 // Only n2 is visible from here on.
where n1.Contains ("x") // Illegal: n1 is not in scope.
select n2;

Projection Strategies

Object Initializers

class TempProjectionItem
public string Original; // Original name
public string Vowelless; // Vowel-stripped name

string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };

IEnumerable<TempProjectionItem> temp =
from n in names select new TempProjectionItem
Original = n,
Vowelless = n.Replace ("a", "").Replace ("e", "").Replace ("i", "")
.Replace ("o", "").Replace ("u", "")

Anonymous Types
We can eliminate the TempProjectionItem class in our previous
example with anonymous types:

var intermediate = from n in names
select new
Original = n,
Vowelless = n.Replace ("a", "").Replace ("e", "").Replace ("i", "")
.Replace ("o", "").Replace ("u", "")
IEnumerable<string> query = from item in intermediate
where item.Vowelless.Length > 2
select item.Original;

Let Keyword

string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };
IEnumerable<string> query =
from n in names
let vowelless = n.Replace ("a", "").Replace ("e", "").Replace ("i", "") .Replace ("o", "").Replace ("u", "")
where vowelless.Length > 2 orderby vowelless
select n; // Thanks to let, n is still in scope.

Interpreted Queries

LINQ provides two parallel architectures: local queries for local object collections, and interpreted queries for remote data sources.

Interpreted queries operate over sequences that implement IQueryable<T>.

combined interpreted and local queries

Friday, July 3, 2015

C# in Nutshell Chapter 7 - Collections

Enumerator and Enumerable

IEnumerable can be thought of as “IEnumeratorProvider”
string s = "Hello";
// Because string implements IEnumerable, we can call GetEnumerator():
IEnumerator rator = s.GetEnumerator();
while (rator.MoveNext())
char c = (char) rator.Current;
Console.Write (c + ".");
// Output: H.e.l.l.o.

Fortunately, you rarely need to write this sort of code, thanks to the foreach statement.

IEnumerable<T> (and IEnumerable)
Provides minimum functionality (enumeration only)

ICollection<T> (and ICollection)
Provides medium functionality (e.g., the Count property)

IList <T>/IDictionary <K,V> and their nongeneric versions
Provide maximum functionality (including “random” access by index/key)

ICollection<T> and ICollection

ICollection<T> is the standard interface for countable collections of objects. 

It provides the ability to determine the size of a collection (Count), determine whether an item exists in the collection (Contains), copy the collection into an array (ToArray), and determine whether the collection is read-only (IsReadOnly). For writable collections, you can also Add, Remove, and Clear items from the collection. And since it extends IEnumerable<T>, it can also be traversed via the foreach statement.

The nongeneric ICollection is similar in providing a countable collection, but doesn’t provide functionality for altering the list or checking for element membership:

IList<T> and IList

IList<T> is the standard interface for collections indexable by position.

In addition to the functionality inherited from ICollection<T> and IEnumerable<T>, it provides the ability to read or write an element by position (via an indexer) and insert/remove by position:


In order to interoperate with read-only Windows Runtime collections, Framework 4.5 introduces a new collection interface called IReadOnlyList<T>.

C# in Nutshell Chapter 6 - Framework Fundamentals


The static string.IsNullOrEmpty method is a useful shortcut for testing whether a given string is either null or empty.

string empty = string.Empty
"abc".IndexOf("a")  // return 0;
"abcdef".StartsWith ("abc", StringComparison.InvariantCultureIgnoreCase)
"abcde".IndexOf ("CD", StringComparison.CurrentCultureIgnoreCase); // 2

LastIndexOf is like IndexOf, but works backward through the string.

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

Console.Write ("ab,cd ef".IndexOfAny (new char[] {' ', ','} )); // 2
Console.Write ("pas5w0rd".IndexOfAny ("0123456789".ToCharArray() )); // 3

LastIndexOfAny does the same in the reverse direction.

Manipulating strings

string left3 = "12345".Substring (0, 3); // left3 = "123";
string mid3 = "12345".Substring (1, 3); // mid3 = "234";
string end3 = "12345".Substring (2); // end3 = "345";

string s1 = "helloworld".Insert (5, ", "); // s1 = "hello, world"
string s2 = s1.Remove (5, 2); // s2 = "helloworld";

PadLeft and PadRight pad a string to a given length with a specified character (or a space if unspecified). If the input string is longer than the padding length, the original string is returned

Console.WriteLine ("12345".PadLeft (9, '*')); // ****12345
Console.WriteLine ("12345".PadLeft (9)); // 12345

TrimStart and TrimEnd remove specified characters from the beginning or end of a string; Trim does both. By default, these functions remove whitespace characters (including spaces, tabs, new lines, and Unicode variations of these): 

Console.WriteLine (" abc \t\r\n ".Trim().Length); // 3

Replace replaces all (non-overlapping) occurrences of a particular character or substring:

Console.WriteLine ("to be done".Replace (" ", " | ") ); // to | be | done
Console.WriteLine ("to be done".Replace (" ", "") ); // tobedone

ToUpper() ToLower() ToUpperInvariant() ToLowerInvariant()

string[] words = "The quick brown fox".Split();
string together = string.Join (" ", words); // The static Join method does the reverse of Split

The static Concat method is similar to Join but accepts only a params string array and applies no separator. Concat is exactly equivalent to the + operator.

string sentence = string.Concat ("The", " quick", " brown", " fox");
string sameSentence = "The" + " quick" + " brown" + " fox";


string composite = "It's {0} degrees in {1} on this {2} morning";
string s = string.Format (composite, 35, "Perth", DateTime.Now.DayOfWeek);
// s == "It's 35 degrees in Perth on this Friday morning"

Each number in curly braces is called a format item. The number corresponds to the argument position and is optionally followed by:
  • A comma and a minimum width to apply
  • A colon and a format string
The minimum width is useful for aligning columns. 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));

The equivalent without using string.Format is this:
string s = "Name=" + "Mary".PadRight (20) + " Credit Limit=" + 500.ToString ("C").PadLeft (15);

Comparing Strings

There are two basic algorithms for string comparison: ordinal and culture-sensitive. Ordinal comparisons interpret characters simply as numbers (according to their numeric Unicode value); culture-sensitive comparisons interpret characters with reference to a particular alphabet. There are two special cultures: the “current culture,” which is based on settings picked up from the computer’s control panel, and the “invariant culture,” which is the same on every computer (and closely matches
American culture).

Despite ordinal’s limitations, string’s == operator always performs ordinal case sensitive 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.

The following methods allow culture-aware or case-insensitive comparisons:
  • public bool Equals(string value, StringComparison comparisonType);
  • public static bool Equals (string a, string b, StringComparison comparisonType);

The static version is advantageous in that it still works if one or both of the strings are null. StringComparison is an enum defined as follows:

public enum StringComparison
CurrentCulture, // Case-sensitive
InvariantCulture, // Case-sensitive
Ordinal, // Case-sensitive

String order comparison

String’s CompareTo instance method performs culture-sensitive, case-sensitive order comparison. Unlike the == operator, CompareTo does not use ordinal comparison: for ordering, a culture-sensitive algorithm is much more useful. Here’s the method’s definition:

public int CompareTo (string strB);

For other kinds of comparison, you can call the static Compare and CompareOrdinal
  1. public static int Compare (string strA, string strB, StringComparison comparisonType);
  2. public static int Compare (string strA, string strB, bool ignoreCase, CultureInfo culture);
  3. public static int Compare (string strA, string strB, bool ignoreCase);
  4. public static int CompareOrdinal (string strA, string strB);

Console.WriteLine ("Boston".CompareTo ("Austin")); // 1
Console.WriteLine ("Boston".CompareTo ("Boston")); // 0
Console.WriteLine ("Boston".CompareTo ("Chicago")); // −1
Console.WriteLine ("ṻ".CompareTo ("ǖ")); // 0
Console.WriteLine ("foo".CompareTo ("FOO")); // −1

By supplying a CultureInfo object, you can plug in any alphabet:
// CultureInfo is defined in the System.Globalization namespace

CultureInfo german = CultureInfo.GetCultureInfo ("de-DE");
int i = string.Compare ("Müller", "Muller", false, german);


The StringBuilder class (System.Text namespace) represents a mutable (editable) string. With a StringBuilder, you can Append, Insert, Remove, and Replace substrings without replacing the whole StringBuilder.

StringBuilder’s constructor optionally accepts an initial string value, as well as a starting size for its internal capacity (default is 16 characters). If you go above this, StringBuilder automatically resizes its internal structures to accommodate (at a slight performance cost) up to its maximum capacity (default is int.MaxValue). A popular use of StringBuilder is to build up a long string by repeatedly calling Append.

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 50; i++) sb.Append (i + ",");
Console.WriteLine (sb.ToString())

In our example above, the expression i + "," means that we’re still repeatedly concatenating strings. However, this incurs only a small performance cost in that the strings in question are small
and don’t grow with each loop iteration. For maximum performance, however, we could change the loop body to this: { sb.Append (i.ToString()); sb.Append (","); }

Methods: AppendLine, AppendFormat, Remove, Insert, Replace

Encoding object

Encoding utf8 = Encoding.GetEncoding ("utf-8");

Encoding chinese = Encoding.GetEncoding ("GB18030");

The most common encodings can also be obtained through dedicated static prop-
erties on Encoding:

  1. Encoding.UTF8
  2. Encoding.Unicode
  3. Encoding.UTF32
  4. Encoding.ASCII

foreach (EncodingInfo info in Encoding.GetEncodings())
    Console.WriteLine (info.Name);

Dates and Times


A TimeSpan represents an interval of time—or a time of the day


  1. public TimeSpan (int hours, int minutes, int seconds);
  2. public TimeSpan (int days, int hours, int minutes, int seconds);
  3. public TimeSpan (int days, int hours, int minutes, int seconds, int milliseconds);
  4. public TimeSpan (long ticks);  // Each tick = 100ns
Or more convenient to use static From... method:

  • public static TimeSpan FromDays (double value);
  • public static TimeSpan FromHours (double value);
  • public static TimeSpan FromMinutes (double value);
  • public static TimeSpan FromSeconds (double value);
  • public static TimeSpan FromMilliseconds (double value);
For example:
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:

TimeSpan.FromHours(2) + TimeSpan.FromMinutes(30);
TimeSpan.FromDays(10) - TimeSpan.FromSeconds(1);  // 9.23:59:59

TimeSpan can also be used to represent the time of the day (the elapsed time since midnight). To obtain the current time of day, call DateTime.Now.TimeOfDay .

DateTime and DateTimeOffset

A DateTime incorporates a three-state flag indicating whether the DateTime is relative to:
  • The local time on the current computer
  • UTC (the modern equivalent of Greenwich Mean Time)
  • Unspecified
A DateTimeOffset is more specific—it stores the offset from UTC as a TimeSpan:

July 01 2007 03:00:00 −06:00

This influences equality comparisons, which is the main factor in choosing between DateTime and DateTimeOffset. Specifically:
  • DateTime ignores the three-state flag in comparisons and considers two values equal if they have the same year, month, day, hour, minute, and so on.
  • DateTimeOffset considers two values equal if they refer to the same point in time.
DateTime dt1 = new DateTime (2000, 1, 1, 10, 20, 30, DateTimeKind.Local);
DateTime dt2 = new DateTime (2000, 1, 1, 10, 20, 30, DateTimeKind.Utc);
Console.WriteLine (dt1 == dt2);
DateTime local = DateTime.Now; // True
DateTime utc = local.ToUniversalTime();
Console.WriteLine (local == utc); // False

DateTime considers the following two values different, whereas DateTimeOffset
considers them equal:
July 01 2007 09:00:00 +00:00 (GMT)
July 01 2007 03:00:00 −06:00 (local time, Central America)

Convert from DateTime to DateTimeOffset

You can construct a DateTimeOffset from an existing DateTime either by using these

public DateTimeOffset (DateTime dateTime);
public DateTimeOffset (DateTime dateTime, TimeSpan offset);

or with an implicit cast. The implicit cast from DateTime to DateTimeOffset is handy because most of the .NET Framework supports DateTime—not DateTimeOffset.

DateTimeOffset dt = new DateTime (2000, 2, 3);

If you don’t specify an offset, it’s inferred from the DateTime value using these rules:

  • If the DateTime has a DateTimeKind of Utc, the offset is zero.
  • If the DateTime has a DateTimeKind of Local or Unspecified (the default), the offset is taken from the current local time zone.

Convert from DateTimeOffset to DateTime

DateTimeOffset provides three properties that return values of type DateTime :

  1. The UtcDateTime property returns a DateTime in UTC time.
  2. The LocalDateTime property returns a DateTime in the current local time zone (converting it if necessary).
  3. The DateTime property returns a DateTime in whatever zone it was specified, with a Kind of Unspecified (i.e., it returns the UTC time plus the offset).

TimeZone and TimeZoneInfo

TimeZoneInfo was introduced in Framework 3.5. 
The biggest difference between the two types is that TimeZone lets you access only the current local time zone, whereas TimeZoneInfo provides access to all the world’s time zones. Further, TimeZoneInfo exposes a richer (although at times, more awkward) rules-based model for describing daylight saving time.


Framework 4.0 introduced a new set of generic classes for holding a set of differently
typed elements. These are called tuples.

You can instantiate a tuple either via its constructor or via the static helper method Tuple.Create:

var t = new Tuple<int,string> (123, "Hello");
Tuple<int,string> t = Tuple.Create (123, "Hello");

You can then access the properties as follows (notice that each is statically typed):

Console.WriteLine (t.Item1 * 2);  // 246
Console.WriteLine (t.Item2.ToUpper());  // HELLO

Tuples are convenient in returning more than one value from a method—or creating
collections of value pairs (we’ll cover collections in the following chapter).

An alternative to tuples is to use an object array. However, you then lose static type
safety, incur the cost of boxing/unboxing for value types, and require clumsy casts
that cannot be validated by the compiler:

object[] items = { 123, "Hello" };
Console.WriteLine ( ((int) items[0]) * 2) ;
Console.WriteLine ( ((string) items[1]).ToUpper() );

Comparing Tuples:

var t1 = Tuple.Create (123, "Hello");
var t2 = Tuple.Create (123, "Hello");
Console.WriteLine (t1 == t2);  // False
Console.WriteLine (t1.Equals (t2));  // True


The static Guid.NewGuid method generates a unique Guid:

Guid g = Guid.NewGuid ();
Console.WriteLine (g.ToString());  // 0d57629c-7d6e-4847-97cb-9e2fc25083fe

To instantiate an existing value, you use one of the constructors. The two most useful constructors are:

public Guid (byte[] b);  // Accepts a 16-byte array
public Guid (string g);  // Accepts a formatted string

Being a struct, a Guid honors value-type semantics; hence, the equality operator works.
The static Guid.Empty property returns an empty Guid (all zeros). This is often used in place of null .

Equality Comparison

There are three standard protocols that types can implement for equality comparison:

  1. The == and != operators
  2. The virtual Equals method in object
  3. The IEquatable<T> interface

Because object is a class (and so a reference type), object’s == operator uses referential equality to compare x and y. The result is false, because x and y each refer to different boxed objects on the heap. "==" and "!=" are operators, i.e. static and resolved at compiling time.

int x = 5, y = 5;

Console.WriteLine (x == y); // True

object x = 5, y = 5;
Console.WriteLine (x == y); // False

Object.Equals method is resolved at runtime - according to the object's actual type. In this example below, it calls Int32’s Equals method, which applies value equality to the operands, returning true. With reference types, Equals performs referential equality comparison by default; with structs, Equals performs structural comparison by calling Equals on each of its fields.

object x = 5, y = 5;
Console.WriteLine (x.Equals (y)); // True

Occasionally, you need to force referential equality comparison. The static object.ReferenceEquals method

Since the Equals method has a parameter of type Object, a cast will be needed in order to be able to access class specific members. Tus A consequence of calling object.Equals is that it forces boxing on value types. This is undesirable in highly performance-sensitive scenarios because boxing is relatively expensive compared to the actual comparison.

The idea is that IEquatable<T>, when implemented, gives the same result as calling object’s virtual Equals method—but more quickly. Most basic .NET types implement IEquatable<T>. You can use IEquatable<T> as a constraint in a generic type:

class Test<T> where T : IEquatable<T>
public bool IsEqual (T a, T b)
return a.Equals (b); // No boxing with generic T

If we remove the generic constraint, the class would still compile, but a.Equals(b) would instead bind to the slower object.Equals (slower assuming T was a value type).

Note. the runtime will use the IEquatable.Equals method only if you pass an object with the same type as defined in the interface implementation; otherwise, it will use the Object.Equals method.
Therefore, in order to align the same result of both interface and object equals() method, we need to do both work:

Order Comparison

The basic protocols for determining the order of objects:

  • The IComparable interfaces (IComparable and IComparable<T>)
  • The > and < operators

The IComparable interfaces are defined as follows:

  • public interface IComparable { int CompareTo (object other); }
  • public interface IComparable<in T> { int CompareTo (T other); }

Wednesday, July 1, 2015

C# in Nutshell Chapter 4 Advanced C#


A delegate is an object that knows how to call a method.

delegate int Transformer (int x);
class Test
static void Main()
Transformer t = Square;  // Create delegate instance
int result = t(3);    // Invoke delegate
Console.WriteLine (result);  // 9
static int Square (int x) { return x * x; }

plug-in methods with Delegates 

public delegate int Transformer (int x);
class Util
public static void Transform (int[] values, Transformer t)
for (int i = 0; i < values.Length; i++)
values[i] = t (values[i]);
class Test
static void Main()
int[] values = { 1, 2, 3 };
Util.Transform (values, Square);  // Hook in the Square method
foreach (int i in values)
Console.Write (i + " ");   // 1 4 9

static int Square (int x) { return x * x; }

Multicast Delegates

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;

Note. Delegates are immutable, so when you call += or −=, you’re in fact creating a new delegate instance and assigning it to the existing variable.

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.

Multicast example:

class Test
static void Main()
ProgressReporter p = WriteProgressToConsole;
p += WriteProgressToFile;
Util.HardWork (p);
static void WriteProgressToConsole (int percentComplete)
Console.WriteLine (percentComplete);
static void WriteProgressToFile (int percentComplete)
System.IO.File.WriteAllText ("progress.txt",

Delegate v.s Interface

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.

Delegate Parameter compatibility

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.

Delegate Return type compatibility

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, a
delegate target method may return a more specific type than described by the delegate.
This is called covariance.

Events in Delegates

The main purpose of events is to prevent subscribers from interfering with each other.
For example below, if we remove the event keyword from our example so that PriceChanged becomes an ordinary delegate field, our example would give the same results. However, Stock
would be less robust, in that subscribers could do the following things to interfere
with each other:
  1. Replace other subscribers by reassigning PriceChanged (instead of using the += operator).
  2. Clear all subscribers (by setting PriceChanged to null).
  3. Broadcast to other subscribers by invoking the delegate.
Code within the Stock type has full access to PriceChanged and can treat it as a delegate. Code outside of Stock can only perform += and −= operations on the PriceChanged event.

public delegate void PriceChangedHandler (decimal oldPrice, decimal newPrice);

public class Stock
string symbol;
decimal price;
public Stock (string symbol) { this.symbol = symbol; }
public event PriceChangedHandler PriceChanged;
public decimal Price
get { return price; }
if (price == value) return; // Exit if nothing has changed
decimal oldPrice = price;
price = value;
if (PriceChanged != null) // If invocation list not
PriceChanged (oldPrice, price); // empty, fire event.

throw v.s. throw ex

try { ... }
catch (Exception ex)
// Log error
throw; // Rethrow same exception

If we replaced throw with throw ex, the example would still work, but the StackTrace property of the newly propagated exception would no longer reflect the original error.

Preprocessor Directives