Immutable isn’t just for parallel code

For the last 6 months the BCL team has been hard at work shipping an out of band release of immutable collections for .Net.  Most recently delivering an efficient implementation of ImmutableArray<T> http://blogs.msdn.com/b/dotnet/archive/2013/06/24/please-welcome-immutablearray.aspx Unfortunately with every announcement around these collections I keep seeing the following going past in my twitter feed. That looks neat.  Next…

5

Don’t mix await and compound assignment

The 5.0 release of C# introduced the await keyword which makes it extremely easy to use Task<T> in a non-blocking fashion.  This allows developers to replace either blocking calls to Task.Wait() or complicated combinations of ContinueWith and callbacks with a nice simple, straight forward expression Task<int> task = …; int local = await task; Use(local);…

7

DebuggerDisplay attribute best practices

The DebuggerDisplayAttribute is a powerful way to customize the way values are displayed at debug time.  Instead of getting a simple type name display, interesting fields, properties or even custom strings can be surfaced to the user in useful combinations [DebuggerDisplay("Student: {FirstName} {LastName}")] public sealed class Student { public string FirstName { get; set; }…

1

Yet another way to defeat C++ const

One of my favorite C++ features, and one I feel is terribly underutilized in many code bases, is the const mechanism.  It’s a great mechanism for defining dual roles for the same object type: a mutable and (ideally) non-mutable one.  But as useful as const is it’s also very easy to circumvent and I’m always…

2

Why the debugging difference between C# and VB.Net return values?

A feature which seems to be getting more requests recently is support for seeing the return value of a function in the debugger without the need to assign it into a temporary.  C++’s had this feature for some time but it’s been lacking in managed debugging scenarios.  James Manning recently dedicated a couple of blog…

0

Discriminated Unions in C++

Earlier this week I started writing a function which needed to represent three states in the return value, two of which had an associated value.  In my mind I immediately came to the following solution type BuildAction = | Reset | LinkOneWithNext of Statement | LinkManyWithNext of Statement seq A discriminated union is perfectly suited…

4

Converting System.Func<T1..TN> to FSharpFunc<T,TResult>

Interop of delegate style types between F# and other .Net languages is a pain point that results from a fundamental difference in how delegates are represented in the F# language.  Typical .Net languages like C# see a delegate as taking 0 to N parameters and potentially returning a value.  F# represents all exposed delegates as…

3

Extension Methods and the Debugger

One source of confusion I find myself clearing up a lot is the use of evaluating extension methods in the debugger windows.  Users report evaluation as working sometimes but not others for the exact same piece of code.  Such flaky behavior can only be the result of a poorly implemented feature or subtle user error. …

6

Disable copy construction when the type is not copy safe

A couple of days ago I finished coding up a feature in our C++ code base, hit F5 and was met with a nasty memory corruption debugger dialog.  After about an hour of investigation it appeared one of my types was living past the lifetime of it’s owning heap. I decided the next step was…

5

Do not throw a NullReferenceException when validing “this” in an extension method

One pattern I’ve started running into is developers explicitly throwing a NullReferenceException when validating the “this” parameter of an extension method. public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action) { if (null == enumerable) { throw new NullReferenceException(); } // rest omitted } The desired behavior here is to make extension methods look even more…

3