I have covered the IDisposable interface in my previous posts IDisposable and using in C# and Implementing IDisposable. To make the implementation of IDisposable easier, I have written an abstract base class that handles the details of the IDisposable pattern. The objectives of the class is to: Provide a simple, reusable implementation of the disposable pattern. Pass all… Continue reading Disposable Base Class
Category: C#
Implementing IDisposable
In the IDisposable and using in C# post I showed how to handle an object that implements IDisposable. That’s the most common scenario, where a simple using will ensure that resources are properly and early released, but that only handles the case when the resource is created and disposed of in the same function. What… Continue reading Implementing IDisposable
Return IEnumerable with yield return
Ever needed to return an IEnumerable<T> from a method? Did you create a List<T> instance that you populated and returned? There is a better way, with less memory footprint and better performance. The yield return statement is one of the more mysterious, yet very useful constructs in C#. With yield return it is possible to… Continue reading Return IEnumerable
Using Transactions for Unit Tests
Unit tests should preferably be independent of external services, systems and files. The standard way to achieve this is to create mocks. A mock is an object that can be used in place of the real resource and act in a predictable way to ensure the tests always give the same result. I think that this is… Continue reading Using Transactions for Unit Tests
IDisposable and using in C#
C# and the .NET environment have automatic memory management through garbage collection. Coming from C++ I think it’s great to not having to worry about memory deallocation. Unfortunately memory from the managed heap (which is where C# objects are placed) is just one kind of resource in a system. There are several other types of… Continue reading IDisposable and using in C#