Intro The purpose of this post is to put in practice and crystallise what we learnt in the Reactive Programming post. The deliverable of this post is a desktop application built with WPF (MVVM) and Rx. However, the concepts apply to any UI framework; web, mobile, or desktop. If you want to know more about reactive…
Insights on Linq Count() vs Count Property
In this post I am investigating the difference between Count property and Count() extension method of the .NET enumerable. Below code snippet sets the scene of what we trying to answer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
static void Main(string[] args) { IList<int> ages = ReadAges(); int ageCount = ages.Count; //Interface Property //OR int ageLinqCount = ages.Count(); //Linq extension IEnumerable<string> names = ReadNames(); var nameCount = names.Count(); //Linq extension; only option } private static IEnumerable<string> ReadNames() { //... } private static IList<int> ReadAges() { //... } |
As in the previous post will start by decompiling the System.Core 4.0.0.0 dll.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
//Count() extension method implementation public static int Count<TSource>(this IEnumerable<TSource> source) { if (source == null) { throw Error.ArgumentNull("source"); } ICollection<TSource> collection = source as ICollection<TSource>; if (collection != null) { return collection.Count; } ICollection collection2 = source as ICollection; if (collection2 != null) { return collection2.Count; } int num = 0; checked { using (IEnumerator<TSource> enumerator = source.GetEnumerator()) { while (enumerator.MoveNext()) { num++; } } return num; } } |
The Linq Count() extension method behaves differently based on the…
Insights on Linq Any() Performance
The Enumerable.Any<TSource>() is an extension method that lives in System.Core assembly under System.Linq namespace. If you are wondering what happens with the extended enumerable source then this is the right post. In the right gets enumerated or not then we would have to look at the implementation. In System.Core 4.0.0.0 the Any(…) method comes with two overloads which is worth investigating. Simply…
Mixed Form Concurrency
In the Insights on .NET Concurrency series we covered three main forms of concurrency in that can be used to solve different problems. Each form has its own strengths to solve problems of this complex world. The right tool for the right job. Parallel programming using TPL offers easy way of parallelising operations in order to increase…
Infragistics XamDataGrid – Unit Testing Dynamic Column Attached Behavior
One of the benefits of adopting WPF Attached Behaviour to adding a dynamic behaviour to WPF controls is that you could unit test the implementation and still use MVVM. The idea of this post is to demonstrate how to setup and run tests for a attached behaviours. This testing is for the implementation covered in an…
Infragistics XamDataGrid – Dynamic Field Group using Attached Behavior
In this post i ll show how to dynamically create grouped columns. This new feature was introduced in Infragitistcs WPF v16.1. This post is based on the previous one, so if you have not read it yet you might want to do so. The attached behaviour class is more complex in this scenario to support the hierarchical…
Insights on .NET Concurrency: Asynchronous Programming
Intro In this post of the concurrency series we will provide insights on asynchronous programming. So what is asynchrony? Succinctly, is about not blocking the thread that initiates an operation. The key difference between synchronous and asynchronous is that the latter can start a new operation before the first one completes. For example, a user clicks on a button to fetch…
The Janus Task Class
Intro The msdn API reference defines the Task class as a representation of an asynchronous operation. However, as soon as you start writing Task based code for asynchronous/parallel programming, or simply offloading work to a thread pool thread, it becomes obvious that Task has more than one face based on the use case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/* 1- Offloading work to a ThreadPool thread */ Task<int> task = new Task<int>(() => SumMethod()); public int SumMethod() { int sum; //... return sum; } /* 2- Asynchronous */ int task = await SumMethodAsync(); public Task<int> SumMethodAsync() { int sum; //... return sum; } |
Task Faces Active…
Insights on .NET Concurrency: Parallel Programming
Intro Parallel programming is the next post from the Insights on .NET Concurrency series. Parallelism is about making more than one thing happen at the same time, with the main objective being improve the performance of CPU bound operations. Parallel programming model tries to overcome single core limitations. With parallelism we can process larger more complex problems in a more…
Insights on .NET Concurrency: Introduction
Concurrent programming is becoming more affordable to developers now a days within the .NET ecosystem. I am starting a series of blog posts that will show you how and when to use different forms of concurrency in .NET. In this first post we will make a smooth entry to concurrency by covering motivations, definitions, forms of concurrency,…