Deep Dive on Closure Pitfalls

I’ve blogged about closures in C# and their pitfalls before.  I keep seeing problems with closures–more now that lambdas expressions and statements (“lambdas”) are becoming more widespread–even with experienced developers. So, I’d thought i’d post about some of the details surrounding where the C# compiler generates closures in the hopes that people will recognize more where they write code that creates a closure and its context. The C# language spec does not refer specifically to “closures”, with regard to capturing state for anonymous methods (including lambdas)–it refers to “outer variables” and “captured outer variables”.  The captured outer variables for a specific … Continue reading Deep Dive on Closure Pitfalls

DataContractSerializer.ReadObject is easily confused.

With WCF services you need to declare contracts and generate contract classes that encapsulate those contracts.  Most of the time you can simply let the framework deal with whatever it needs to do to deal with these objects.  Sometimes, you need to actually see without running a service what XML would result from a contract object or serialize a contract object from XML text. In .NET 3.5 there exists the System.Runtime.Serialization.DataContractSerializer class that perform serialization of data contracts. Based on its documentation it seems fairly simple to create data contract object to/from XML methods.  For example:     public static T … Continue reading DataContractSerializer.ReadObject is easily confused.

DevTeach 2009 Vancouver

The schedule for DevTeach 2009 Vancouver has been announced (  There’s lots of great software development sessions from some of the leaders in our industry. If you’re planning on improving yourself, this is the conference to go to.  Not only can you attend excellent sessions; but you can hob-knob with the presenters and pick their brains. If you have a friend or co-worker who’s interested, there’s a limited-time two-for-one offer for an even better price:

Trials and Tribulations of DataGridView, Column Selections, and Sorting

I had to implement some custom sorting in a DataGridView recently.  Essentially, the stakeholders wanted full column selection (like Excel) while still having the ability to sort the data based on a particular column. This particular DataGridView is data-bound.  DataGridView offers the Sort(DataGridViewColumn, ListSortDirection) method to perform this.  Nice and easy I thought: I’ll set the SelectionMode to DataGridViewSelectionMode.ColumnHeaderSelect and simply call Sort with the selected column. Well, much to my chagrin this had the side effect of making that column look selected all the time.  No matter where else I clicked, that recently sorted column looked selected (SelectedColumns had … Continue reading Trials and Tribulations of DataGridView, Column Selections, and Sorting

Drag-copying in Visual Studio Solution Explorer.

NOTE: I’ve tried this in Visual Studio 2008 (VS2k8), I’m assuming the same thing happens in Visual Studio 2005 (VS2k5). In the process of refactoring, it’s *very* common for me to rename a type.  This is most easily done by renaming the file in the Solution Explorer (SE)–which renames the file, the type, and and any uses of the type in the entire solution. Occasionally, I need to create a new type based on another.  Copying an abstract type in order to implement the abstract type is often handy–I just fill in the abstract members (and delete “abstract”) in the … Continue reading Drag-copying in Visual Studio Solution Explorer.

Nested Types

Recently Michael Features blogged about nested types.  The title was almost “nested types considered harmful”. I don’t agree.  I don’t agree that they’re any more harmful than any other C# construct (except goto…).  Nested types are like anything else in our tool-belt: they have a time and place and can be abused. But, when to use them?  Well, for the most part I agree with Michael, you should avoid them. But, there are times when they’re simply the best solution in a given set of circumstances. Let’s look at asynchronous programming model (APM) in .NET.         // Paraphrased from … Continue reading Nested Types

Upcoming C# 3 Guidance From Microsoft

Mircea Trofin has some design guidelines with regard to some C# 3 language additions (that I assume will make it into a revised Framework Design Guidelines of some sort).  They more less agree with the guidelines I published in Code Magazine a while ago.  There are some slight differences: Consider using extension methods in any of the following scenarios: to provide helper functionally relevant to every implementation of an interface… and, Do define extension methods in the same namespace as the extended type, if the type is an interface, and if the extension methods are meant to be used in most … Continue reading Upcoming C# 3 Guidance From Microsoft

BigInteger is not in .NET Framework 3.5

A while back Inbar Gazit blogged about a new (at the time) Orcas type System.Numeric.BigInteger.  BitInteger is intended to represent an integer of “arbitrary” precision–from 1 digit to 2,147,483,647 digits.  “Arbitrary” because realisitically approaching 2,147,483,647 will not have enough memory to store the integer. For whatever reason, this type didn’t make it into the RTM version of .NET 3.5–it’s actually internal, not public.  I don’t know why, Inbar didn’t follow up.