How many times in your code do you need to parse strings to primitive values? I would guess pretty often. How often do you use
TryParse method, to avoid unnecessary exception handling? The same guess – probably pretty often. How many times did you wish that
TryParse didn’t use
out method parameter? Yes, the same answer again. But how many times did you think how to improve that scenario? Probably never. I think you should have done that! There is quite a lot of room for improvement here. Simple tricks that may make your parsing logic much simpler, cleaner and more verbatim.
Continue reading Better TryParse method, when you don’t like out parameters and/or need default value
Another blog post inspired by StackOverflow question. This time it’s all about LINQ, performance and a tiny little detail, that really matters. The question itself is about
yield keyword in VB.NET, but there is another, much more interesting part I’d like to examine. The algorithm quoted in the question is the key.
The idea is simple. How to partition a collection into parts with given number of elements in ever part? Algorithm presented in the question is as easy as the questions seems to be:
Continue reading Partitioning the collection using LINQ: different approaches, different performance, the same result
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
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
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
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!