.NET Blog
Free. Cross-platform. Open source. A developer platform for building all your apps.
Featured posts

Announcing .NET 11
.NET 11 is here with C# 15, faster developer tools, runtime and library improvements, and new capabilities for web, mobile, desktop, and AI apps.
Latest posts
Potential pitfalls to avoid when passing around async lambdas
One of the really useful capabilities of the new async methods feature in C# and Visual Basic is the ability to write async lambdas and anonymous methods (from here on in this post, I’ll refer to both of these as async lambdas, since the discussion applies equally to both). This allows you to easily get a delegate to represent an asynchronous operation, e.g. Func<Uri,Task<string>> getContentsLowerCaseAsync = async url => { string contents = await DownloadString(url); return contents.ToLower(); }; Async methods in C# and...
When “ExecuteSynchronously” doesn’t execute synchronously
When creating a task continuation with ContinueWith, developers have the opportunity to provide a TaskContinuationOptions enum value, which could include the TaskContinuationOptions.ExecuteSynchronously flag. ExecuteSynchronously is a request for an optimization to run the continuation task on the same thread that completed the antecedent task off of which we continued, in effect running the continuation as part of the antecedent’s transition to a final state (the default behavior for ContinueWith if this flag isn’t specified is to run the continuation asynchronously, meaning that when the antec...
FromAsync(asyncResult, …) vs FromAsync(beginMethod, …)
The Task Parallel Library (TPL) provides a set of “FromAsync” helper methods that create a Task or a Task<TResult> to represent an invocation of an APM method pair, i.e. BeginXx / EndXx. There are, however, two different flavors among these overloads: ones that accept an IAsyncResult “asyncResult” as the first parameter, and ones that accept a Func<…,AsyncCallback,object,IAsyncResult> “beginMethod” as the first parameter. What’s the difference, and why would you choose one over the other?The fundamental difference between the two is in how t...
Building a custom GetOrAdd method for ConcurrentDictionary
I was recently asked by a developer about getting some additional information out of ConcurrentDictionary<TKey,TValue>’s GetOrAdd method. As a reminder, GetOrAdd either returns the value for a key currently in the dictionary, or if that key doesn’t have a value, it adds a value for the key as dictated by either a TValue provided by the caller or by executing a Func<TKey,TValue> provided by the caller. It then returns the new value. However, it doesn’t tell the caller which happened; all the caller knows is that it’s handed back the value (existing or new) that...
Know Thine Implicit Allocations
For .NET 4.5, we’ve invested quite a bit of effort into performance, and in particular for the Task Parallel Library (Joe Hoag wrote a good paper covering some of these improvements). We focused such effort on TPL because it is a core component used in async programming and at a foundational level for many libraries and applications. Small tweaks to the performance characteristics of TPL can thusly have a noticeable impact on the performance of these higher-level systems.In particular, one of the areas we focused a lot of attention on is memory utilization: how many objects are we allocating, an...
Await, SynchronizationContext, and Console Apps: Part 3
In Part 1 and Part 2 of this short series, I demonstrated how you can build a SynchronizationContext and use it run an async method such that all of the continuations in that method will run on serialized on the current thread. This can be helpful when executing async methods in a console app, or in a unit test framework that doesn’t directly support async methods. However, the support I showed thus far targets async methods that return Task… what about async methods that return void? C# and Visual Basic support two flavors of async methods: ones that return tasks (either Task or Task<...
KB2650605 QFE : VS2010 sp1 VB web form editor may not become editable during debugging
We recently released a VS2010 sp1 QFE KB2650605 to solve a VB editor debugging problem. During VB web form debugging, VB web form may become un-editable if there are server tags inside. If you develop and debug VB web forms, you might be interested to download this QFE. http://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=40811 Update for another download place due to broken link in connect Url: http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-63-56/3146.VS10SP1_2D00_KB2650605_2D00_x86.exe Best regards, Xinyang Qiu Web Platf...
Advanced APM Consumption in Async Methods
I’ve previously blogged about how to expose existing Asynchronous Programming Model (APM) implementations as Task-based methods. This can be done manually using a TaskCompletionSource<TResult>, or it can be done using the built-in wrapper provided in TPL via the Task.Factory.FromAsync method. By creating a Task-based wrapper for such BeginXx/EndXx method pairs, APM implementations become consumable in async methods in C# and Visual Basic: since Tasks are awaitable, any operation exposed as a Task is then inherently awaitable.What I haven’t before discussed is how you can consume an A...
Await, SynchronizationContext, and Console Apps: Part 2
Yesterday, I blogged about how you can implement a custom SynchronizationContext in order to pump the continuations used by async methods so that they may be processed on a single, dedicated thread. I also highlighted that this is basically what UI frameworks like Windows Forms and Windows Presentation Foundation do with their message pumps.Now that we understand the mechanics of how these things work, it’s worth pointing out that we can achieve the same basic semantics without writing our own custom SynchronizationContext. Instead, we can use one that already exists in the .NET Framework: Dispa...