The Power and Peril of Anonymity

Back Draft


The Power and Peril of Anonymity


By Jonathan Goodyear


PDC 2005 was full of new tools and technologies. For a guy like me who writes opinions on the present and future of the Microsoft software developer world, this was very good news I ll have material to chew on for quite some time. In fact, it was very difficult to choose what I wanted to cover this month. If you ll recall, I mentioned a couple of months back that I would be posting a more complete review of the Atlas project, which is Microsoft s foray into AJAX (Asynchronous JavaScript and XML) tools. Although I saw many promising demos at PDC, I m going to hold off on that until I m able to get some additional first-hand experience with it. For now, I ve got other fish to fry.


Any time Anders Hejlsberg ( has something to say, my butt finds a seat to listen. I caught his talk on the LINQ (Language Integrated Query) Project (, as well as his talk on C# v3.0. Both of these are scheduled for the Orcas timeframe (the next version after Visual Studio 2005). In a nutshell, the LINQ Project will allow you to query objects using a strongly typed SQL syntax. For example, you could query an array of strings like this:


string[] names = { "Jon", "Joy", "CJ", "Rylee", "Cherie" };

var expr = from s in names

          where s.Length == 3

          orderby s

          select s.ToUpper();

foreach (string item in expr)



This would produce the following results:





The same functionality will exist in Visual Basic 9, with a few syntax modifications. The related DLinq Project will allow you to perform database operations in a strongly typed environment, and the XLinq Project will give you the same functionality for XML documents. Clearly, this is powerful and ground-breaking technology that will not only greatly reduce the number of lines of code that it takes to manipulate objects and data into usable results, but it will also allow you to catch many run-time exceptions of today at compile time.


To make LINQ work, Microsoft had to make some extensions to the .NET Framework. For example, if you look closely at the example above, you ll notice that the expr variable that receives the result of the LINQ query is defined as type var . At first you might think this is a throwback to the old late-bound variant type. Thankfully, we aren t returning to those days. In fact, the var keyword is how anonymous types are utilized in C# v3.0. Similar functionality exists for Visual Basic 9. The impact of anonymous types is what I d like to concentrate on this month.


What exactly are anonymous types? You may recall that C# v2.0 introduced anonymous methods, which essentially allow you to define nameless methods inline in your code. A common use of anonymous methods is for simple delegate handlers. Anonymous types infer their types from the data you assign to them. For example, take a look at the following few lines of code:


var x = "hello";

var y = 3;

var z = new {Name="Jon", Age=31};


In the code above, x is now equivalent to a strongly typed string, y is equivalent to a strongly typed integer, and z contains a nameless type that has a Name property of type string and an Age property of type integer. One of the great things about anonymous types is that they perform just as well as if you had explicitly defined their type. That s because, internally, they are strongly typed.


Going back to the LINQ query above, it is convenient to use an anonymous type for the return value because it will automatically take on the characteristics of an enumerable string array. Alternatively, you could have defined expr as a string array, or used an IEnumerable<string> generic. However, in cases where your LINQ query will return a custom type, you don t have this luxury. In that case, you must use an anonymous type.


The thing about anonymous types that makes my stomach a little uneasy is that, even though they perform like strong types, their short-hand nature makes them much less visually descriptive. Something about it just screams JavaScript to me. One of the reasons why Hungarian Notation gained popularity in script-based languages is because you could use prefixes to remind yourself what type of data a particular variable or parameter contained. With the introduction of .NET, camel casing became the standard for variable and parameter naming because their type was explicitly defined. In the simple code example above, it s easy to tell that x is a string, because it is initialized with the static string hello ; but what if you initialized it with the result of a function or the property of yet another type (perhaps even an anonymous one)? You could quickly end up on a wild goose chase when you try to debug or maintain that code at a later time. In the case of anonymous complex types (like the third line of code in the example above), your debug window won t even be able to help you figure out its type, because it won t have one. It s strong typed, but nameless.


A second concern I have with anonymous types is that inexperienced developers will begin to use them as a replacement for well-defined business objects. After all, why go through the hassle of pre-defining business objects when you can simply define them on the fly when you need them? An experienced developer can immediately point out numerous holes with the logic in my previous statement, but less experienced developers will most assuredly not be so astute. The next thing you know, you re not only looking at anonymous types, you re looking at anonymous code!


Anonymous types represent both a panacea and a Pandora s Box. They offer the flexibility to enable powerful technologies like LINQ, yet can easily be used in ways that defy their true intentions. Used sparingly, they can provide amazing productivity gains, so I m glad that they are coming in C# v3.0 and Visual Basic 9. I m just not looking forward to the inevitable developer misuse that will accompany the feature. There are some other new language features, such as extension methods that are used to make LINQ work, which also have both power and peril implications, but I m out of room for this month, so they ll have to wait.


If you re still confused, don t worry you ve got plenty of time to figure it all out. I encourage you to download the LINQ beta and get acquainted with it.


Jonathan Goodyear is president of ASPSoft (, an Internet consulting firm based in Orlando, FL. Jonathan is Microsoft Regional Director for Florida, a Microsoft Certified Solution Developer (MCSD), and author of Debugging ASP.NET (New Riders). Jonathan also is a contributing editor for asp.netPRO. E-mail him at mailto:[email protected] or through his angryCoder eZine at




Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.