Csharp Patterns

--- Design Patterns: Solidify Your C# Application Architecture with Design Patterns http://msdn.microsoft.com/msdnmag/issues/01/07/patterns/

Patterns include: Resources

--- A quick summary of the DisposePattern?:

An object that manages resources that need to be explicitly released implements the IDisposable interface, which has a single method, called Dispose, which is implemented something like this:

 protected virtual void Dispose(bool disposing) {
	if(!this.disposed) {
		if(disposing) {
	this.disposed = true;         

From Microsoft's SDK docs:

''Dispose(bool disposing) executes in two distinct scenarios. If disposing equals true, the method has been called directly or indirectly by a user's code. Managed and unmanaged resources can be disposed.

If disposing equals false, the method has been called by the runtime from inside the finalizer and you should not reference other objects. Only unmanaged resources can be disposed.''

So, in the context of something like a database connection, there might be a public Close() method which calls the protected Dispose(true) method, and a Finalize() (or 'destructor') method - called by the runtime during GC - which calls Dispose(false).

And one other factor of the pattern recommended by JeffreyRichter? in Applied Microsoft .Net Programming: If called explicitly, the Dispose method should call GC.SuppressFinalize?(this). Since the Dispose method has freed unmanaged resources, there is no longer need for the garbage collector to call the class's destructor. Because garbage collection is more efficient on non-Finalizable types, the explicitly called IDisposable.Dispose method should suppress the no-longer needed destructor.

In C#, client code manages disposable objects with the "using" keyword. This:

 using (SomeObject? o = new SomeObject?())

is equivalent to this:

 SomeObject? o = new SomeObject?();

CeeSharp makes the Singleton pattern pretty easy:

 sealed class Singleton {
    private Singleton() {}
    public static readonly Singleton Instance = new Singleton();

I found MSDN's explanation of asynchronous programming to be confusing, so I wrote my own notes and example: http://kristopherjohnson.net/kj/AsynchronousDelegate. --KrisJohnson
--- Enterprise Solution Patterns Using Microsoft .NET (.pdf Download)


Yes, DoubleCheckedLockingIsBroken in C#. For the nitty gritty, google for "chris brumme" and "memory barriers". --CharlesReilly?


View edit of September 26, 2014 or FindPage with title or text search