LINQ: from IEnumerable to concrete collections

I my recent posts introducing LINQ from a game developers point of view, I mentioned several times how the many LINQ methods returning sequences of the IEnumerable<T> type do not actually return an actual collection.

Instead they return a query that can be executed any number of time on the given input collection.

Of course, there comes a point at which we need to store the results of such queries as regular collections. Today we will talk about how LINQ supports this almost trivially.

Continue reading →

Sorting and Grouping – organizing data with LINQ

Last week I introduced LINQ from the perspective of a C# game developer completely unfamiliar with the framework. Today I would like to continue exploration of LINQ by focussing on a particular set of its functionality: methods to arrange and organize data.

In particular we will look into how we can sort and group our collections of items.

Continue reading →

LINQ – a game development focused introduction

I was recently asked for some pointers on how to get started with LINQ – and to maybe write a post about that. Using LINQ virtually every day I have to admit that it had not occurred to me that a C# programmer may not be familiar with it.

LINQ is a big topic, but this post is the first in a series to introduce the framework and its many uses – all from a game developer’s point of view.

Continue reading →

LINQ Extensions 3: Batch into sub-sequences

After last weeks post on extracting elements out of a list by minimum or maximum keys Ody Mbegbu mentioned on Google+ how he feels that something LINQ is missing is the functionality to batch, page, or divide a sequence into sub-sequences of a given size.

That is what we are going to look at today!

Continue reading →

LINQ Extensions 2: Minimum/Maximum by key

Last week we talked about LINQ, its usefulness, and how to write our own methods to make it even more powerful. Today, I want to look at another couple of methods that I have found handy in a number of different situations. We will look at how to extract the maximum or minimum element of a list by a given key or selector.

Continue reading →

DIY: Useful LINQ Extensions

LINQ (Language Integrated Query) is one of the most powerful features of modern .NET. Powered by generics, lambda expressions, method chaining, extension methods, and deferred execution it allows to write extremely concise code when dealing with collections.

In this post we will look some useful LINQ extensions I have written over the years to make my work with LINQ even easier and quicker, and help me simplify my code.

Continue reading →

Converting between Structs and Byte Arrays

In object-oriented code bases, we tend to express most of not all of our data in highly semantic and contextual ways – that is, we use classes that contain both data and behaviour, and often even more information through inheritance, attributes, and more.

However, sometimes we need to extract the data contained in these types – for example for sending network messages, or saving to disk. In this post we will look into converting between structs and byte arrays, to make exactly this possible.

We will compare different ways of doing so, and analyse them for performance and easy of use.

Continue reading →

Reusing objects with generic object pooling

Over the last couple of months I’ve been working a lot with WPF (Windows Presentation Foundation), the popular user interface framework by Microsoft.

Something that I noticed quite quickly is how expensive it can be to create WPF controls in code. It could take up to several milliseconds to create a new interface element – even simple ones. The interface I was working on had to be very flexible and could change often however, which would cause it to freeze for noticeable durations regularly, which is unacceptable.

The way I solved that problem is by using object pools.

Continue reading →