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
Category: C#
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#
Data Binding a Single Object when IEnumerable is Expected
Controls that can be used with data binding typically expect some kind of collection as the data source. One example is the ASP.NET DetailsView control. It displays a single record, but still expects a collection (to be exact: something that implements IEnumerable) to be assigned to its DataSource property. I use a small extension method… Continue reading Data Binding a Single Object when IEnumerable
Always Check Generated SQL
In my last post I advocated for using LINQ to SQL for data access. Today I am going to show an example of how the greater power of LINQ compared to SQL sometimes results in terrible performance when LINQ to SQL does it best to work around the limitations of SQL. var q = from… Continue reading Always Check Generated SQL