Playing around with List<T>, part two: IEnumerable and IEnumerable<T> implementation

After a while I finally found some time to write another post about List<T> internals. This time it’s all about enumeration, so I’m going to go through both non-generic IEnumerable and generic IEnumerable<T> interfaces implementation.

It may be a little bit surprising, but there are actually three GetEnumerator() methods within List<T> class. That’s because both IEnumerable.GetEnumerator() and IEnumerable<T>.GetEnumerator() methods are implemented explicitly (and there is another one, not connected to any of the interfaces):
Continue reading Playing around with List<T>, part two: IEnumerable and IEnumerable<T> implementation

Playing around with List<T>, part one: Constructors

List<T> is one of the most commonly used types from .NET Framework Base Class Library. Although it is used so often not everyone is really familiar with how the class works internally. Almost everyone knows that List<T> uses T[] array internally to store the items. But for most people that’s the only internal detail they know. In next few blog post I’ll try to step through List<T> source code and point some interesting implementation details that every .NET developer should be aware of.

Starting from the basics, I’d like to dive deep into List<T> class constructors first. There are three instance constructors available:
Continue reading Playing around with List<T>, part one: Constructors

Why (or when) you should/shouldn’t use Descendants() method

A lot of LINQ to XML questions on StackOverflow are being answered using Descendants() method calls. It looks like most of people think it’s the best way to handle and query XML document: it’s easy to use, you don’t have to worry about whole tree structure. It just works. But are these solutions really good ones? Is Descendants() method really that good as it seems to be? I would say: NO! I think common LINQ to XML queries should not use Descendants(). I’ll try to answer it shouldn’t be used in this blog post.
Continue reading Why (or when) you should/shouldn’t use Descendants() method

Convert class, IConvertible interface and Generics – deep dive into performance

First non LINQ-related question on my blog, but another one in response to great StackOverflow question: Casting generic parameter to and from integer. Every day you can find something that inspires you to do your own research and think hard why things work like they do. That’s exciting, isn’t it?

Getting back to question. Author posted following code and simply asked, how can it be done better?
Continue reading Convert class, IConvertible interface and Generics – deep dive into performance

Querying hierarchical data using LINQ to Object

Another blog post in response to a StackOverflow question. It’s about LINQ again, but it’s no as low-level as the one from previous post. This time the question is quite simple: How to search Hierarchical Data with Linq.

To answer the question, let’s start with some sample data we could fight against, starting with Person class:
Continue reading Querying hierarchical data using LINQ to Object

Did you know about that LINQ feature? I guess you didn’t!

First of all, I have to say thank you! to Toto for Most optimized use of multiple Where statements StackOverflow question and hatchet for really great answer, which made me write this blog post.

The question is simple: does LINQ optimize multiple Where calls? I think most people would say no. I did say no too! Google says no * unless you dig really deep! But what is the correct answer to that question? Continue reading Did you know about that LINQ feature? I guess you didn’t!