InfoQ

News

C# and VB Continue to Diverge

Posted by Jonathan Allen on May 14, 2007 11:34 AM

Community
.NET
Topics
.NET Framework ,
Programming
Tags
LINQ ,
C# ,
Visual Basic.NET

When VB.NET and C# were first released, they were often thought of as the same language with a different syntax and minor differences. As time goes on, these differences are becoming more pronounced. For example, their treatment of anonymous types is worlds apart.

In order to support data structures like hash tables and query operations like grouping, anonymous types created by LINQ must provide stable hash codes. Hash codes are normally created from the fields in the object.

The early versions of anonymous types were mutable. Or in other words, the values contained by the object could be altered. Changing these values would change the hash code, which would in turn break any hash table or dictionary that the object happened to be stored in.

Taking the direct route, the C# team made anonymous types immutable. If the object cannot be altered, then the hash code will never change either. The usually rules for immutable types were put into place such as no default constructors and only getters for properties.

The VB team was not so willing to give up the ability to modify anonymous classes. Paul Vick writes 

In looking at this problem, though, we didn't want to throw the baby out with the bathwater. Anonymous types are somewhat limited at the moment because they cannot be named, but you can use late binding to work with them even outside of the context in which they were declared. And future features that we're interested in exploring, such as nominal anonymous types and dynamic interfaces, may make anonymous types even more useful. As such, it seemed too drastic to simply make them immutable, especially because this would be a one-way decision--once they were immutable, compatibility would make it extremely difficult to make them mutable again in the future if it become more desirable to do so.

They choose a solution that is more complex, but also gives the developer more flexibility. When creating anonymous classes, programmers can use the modifier "Key" to indicate which fields are immutable. In addition to making the property read-only, the hash code function will only use these 'key' fields to generate the hash code. As a result, the hash codes are guaranteed to be stable. Fields will also be automatically marked as Key by the compiler when used in joins and group by clauses.

The reason VB and C# can use different implementations is that anonymous types are a compiler feature. The CLR itself doesn't have any notion of anonymous types and sees them as normal classes with automatically generated names.

Like much of the VB syntax, this functionality is expected to not be available until Orcas Beta 2.

No comments

Reply

Exclusive Content

Book Except and Interview : Aptana RadRails, An IDE for Rails Development

Aptana RadRails: An IDE for Rails Development by Javier Ramírez discusses the latest Aptana RadRails IDE, a development environment for creating Ruby on Rails applications.

Fast Bytecodes for Funny Languages

Cliff Click discusses how to optimize generated bytecode for running on the JVM. Click analyzes and reports on several JVM languages and shows several places where they could increase performance.

Scott Ambler On Agile’s Present and Future

Scott Ambler, Practice Lead for Agile Development at IBM, speaks on the current status of the Agile community and practices having a look at the perspective of the Agile’s future.

Manager's Introduction to Test-Driven Development

Dave Nicolette and Karl Scotland try to introduce non-technical managers to one of the most popular Agile development techniques: Test-Driven Development (TDD).

Structured Event Streaming with Smooks

Smooks is best known for its transformation capabilities, but in this article Tom Fennelly describes how you can also use it for structured event streaming.

How to Work With Business Leaders to Manage Architectural Change

Successful architectures evolve over time to meet changing business requirements. Luke Hohmann presents how to collaborate with key members of your business to manage architectural changes.

Colors and the UI

In this article, Dr. Tobias Komischke explains how colors used in a GUI can influence our interaction with a computer and offers advice on using the appropriate colors for the interface.

Building your next service with the Atom Publishing Protocol

In his presentation, recorded at QCon San Francisco, MuleSource architect Dan Diephouse explores ways to use the Atom Publishing Protocol (AtomPub) when building services in a RESTful way.