InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

C# and VB Continue to Diverge

Posted by Jonathan Allen on May 14, 2007

Sections
Development,
Architecture & Design
Topics
.NET ,
.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

Watch Thread Reply

Educational Content

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.

Beauty Is in the Eye of the Beholder

Alex Papadimoulis discusses ugly code, where it comes from, how to avoid it, and how to get rid of it.

Architecting Visa for Massive Scale and Continuous Innovation

John Davies examines Visa’s architecture and shows how enterprises have architected complex integrations incorporating Hadoop, memcached, Ruby on Rails, and others to deliver innovative solutions.

Max Protect: Scalability and Caching at ESPN.com

Sean Comerford unveils ESPN.com’s architecture, what components are used and why, and the current changes the website goes through.

The Seven Deadly Sins of Enterprise Agile Adoption

Are there repeated patterns of failure on Enterprise Agile Enablement efforts? Sanjiv and Arlen discuss Seven Deadly Sins to avoid when adopting Agile in an enterprise.

Questions for an Enterprise Architect

Erik Dörnenburg answers: What is Enterprise and Evolutionary Architecture?, discussing 4 issues: Turning strategy into execution, Ensuring conformance, Where do the architects sit? Buying or building?

Wrap Your SQL Head Around Riak MapReduce

Sean Cribbs explains what Map-Reduce and Riak are, why and how to use Map-Reduce with Riak, and how to convert SQL queries into their Map-Reduce equivalents.